智能指针

来源:互联网 发布:江宁淳化网络问政 编辑:程序博客网 时间:2024/04/27 01:44

http://en.wikipedia.org/wiki/Auto_ptr    &&  《c++ primer》17.1.9

1 In the upcoming c++0x, auto_ptr will be deprecated;

2 auto_ptr之间的拷贝回导致source失去所指向对象的reference,因为同一指针智能被一个auto_ptr所拥有;

3 auto_ptr使用delete销毁拥有的指针,因此他拥有的指针只能使用new来分配空间。malloc,calloc,realloc分配的指针不能使用,array也不可以,使用的是new[]。

 

 

《more effective c++》

item10, 智能指针能用来解决在构造函数中发生异常导致的内存泄露问题;

 

 

shared_ptr

http://www.cppprog.com/boost_doc/libs/smart_ptr/shared_ptr.htm

1 能够用于stl库;

2 单个对象使用shared_ptr, 动态数组使用shared_array;

3 best practises

   3.1 一个简单的近乎完全消灭内存泄露的方针,总是使用一个已命名的智能指针接收new的结果;

   3.2 不要使用匿名的shared_ptr临时变量存储内容,可能造成内存泄露。

 

原创粉丝点击