From 1d0bc583cc1dd05316cdfec7c44ef9d29323beaf Mon Sep 17 00:00:00 2001 From: traviszeng Date: Wed, 27 Feb 2019 15:14:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0deallocate=E5=92=8Cdestory?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STL/STL.md | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/STL/STL.md b/STL/STL.md index 4deb632..ba52f9b 100644 --- a/STL/STL.md +++ b/STL/STL.md @@ -401,22 +401,22 @@ Example #include #include -int main () +int main() { - std::array myarray; - unsigned int i; + std::array myarray; + unsigned int i; - // assign some values: - for(i=0; i<10; i++) - myarray[i] = i; + // assign some values: + for (i = 0; i<10; i++) + myarray[i] = i; - // print content - std::cout << "myarray contains:"; - for(i=0; i<10; i++) - std::cout << ' ' << myarray[i]; - std::cout << '\n'; + // print content + std::cout << "myarray contains:"; + for (i = 0; i<10; i++) + std::cout << ' ' << myarray.at(i); + std::cout << '\n'; - return 0; + return 0; } ``` Output @@ -1608,6 +1608,33 @@ Output ``` The allocated array contains: 0 1 2 3 4 ``` + +注意:deallocate和destory的关系: + +deallocate实现的源码: + + template + inline void _deallocate(T* buffer) + { + ::operator delete(buffer); //为什么不用 delete [] ? ,operator delete 区别于 delete + //operator delete 是一个底层操作符 + } + +destory: + + template + inline void _destory(T *ptr) + { + ptr->~T(); + } + +destory负责调用类型的析构函数,销毁相应内存上的内容(但销毁后内存地址仍保留) + +deallocate负责释放内存(此时相应内存中的值在此之前应调用destory销毁,将内存地址返回给系统,代表这部分地址使用引用-1) + + + + #### relational operators (vector) #### swap (vector) #### vector