智能指针 boost(scoped_ptr,scoped_array,shared_ptr,shared_array) 和 std (auto_ptr)的比较 .
来源:互联网 发布:陈正生洞箫f调数据 编辑:程序博客网 时间:2024/05/18 03:39
http://blog.csdn.net/afrish/article/details/3985471
智能指针 boost(scoped_ptr,scoped_array,shared_ptr,shared_array) 和 std (auto_ptr)的比较
1、std::auto_ptr
std::auto_ptr相信都接触使用过,对于一般的用户它绝对够用了,new一块内存自动释放
例如:
- int main()
- {
- std::auto_ptr<int> p(new int);
- return 0;
- }
但它的不足之处在与,对象拥有者只有一个,换句话说:就是new出来的内存空间只属于一个对象
例如:
- int main()
- {
- std::auto<str::string> p(new string("123"));
- std::auto<str::string> p1(p);
- cout << p << endl; // p的值已近为空
- cout << p1 << endl; // 123
- }
2、scoped_ptr、scoped_array
scoped_ptr与auto_ptr相似, scoped_ptr智能指针与std::auto_ptr不同在于,它明确禁止任何想要这样做的企图!这在你需要确保指针任何时候只有一个拥有者时的任何一种情境下都是非常重要的
scoped_ptr通过成员还是的private私有化来禁止拷贝 nocopyable
- class noncopyable
- {
- protected:
- noncopyable() {}
- ~noncopyable() {}
- private: // emphasize the following members are private
- noncopyable( const noncopyable& );
- const noncopyable& operator=( const noncopyable& );
- };
scoped_array与scoped_ptr显然是意义等价的,但是是用来处理数组的
3、shared_ptr,shared_array
boost::scoped_ptr虽然简单易用,但它不能共享所有权的特性却大大限制了其使用范围,而 boost::shared_ptr可以解决这一局限。顾名思义,boost::shared_ptr是可以共享所有权的智能指针,首先让我们通过一个例子看看它的基本用法:
- #include <string>
- #include <iostream>
- #include <boost/shared_ptr.hpp>
- class implementation
- {
- public:
- ~implementation() { std::cout <<"destroying implementation/n"; }
- void do_something() { std::cout << "did something/n"; }
- };
- void test()
- {
- boost::shared_ptr<implementation> sp1(new implementation());
- std::cout<<"The Sample now has "<<sp1.use_count()<<" references/n";
- boost::shared_ptr<implementation> sp2 = sp1;
- std::cout<<"The Sample now has "<<sp2.use_count()<<" references/n";
- sp1.reset();
- std::cout<<"After Reset sp1. The Sample now has "<<sp2.use_count()<<" references/n";
- sp2.reset();
- std::cout<<"After Reset sp2./n";
- }
- void main()
- {
- test();
- }
该程序的输出结果如下:
The Sample now has 1 references
The Sample now has 2 references
After Reset sp1. The Sample now has 1 references
destroying implementation
After Reset sp2.
可以看到,boost::shared_ptr指针sp1和sp2同时拥有了implementation对象的访问权限,且当sp1和sp2都释放对该对象的所有权时,其所管理的的对象的内存才被自动释放。在共享对象的访问权限同时,也实现了其内存的自动管理。
boost::shared_ptr的内存管理机制:
boost::shared_ptr的管理机制其实并不复杂,就是对所管理的对象进行了引用计数,当新增一个boost::shared_ptr对该对象进行管理时,就将该对象的引用计数加一;减少一个boost::shared_ptr对该对象进行管理时,就将该对象的引用计数减一,如果该对象的引用计数为0的时候,说明没有任何指针对其管理,才调用delete释放其所占的内存。
上面的那个例子可以的图示如下:
- sp1对implementation对象进行管理,其引用计数为1
- 增加sp2对implementation对象进行管理,其引用计数增加为2
- sp1释放对implementation对象进行管理,其引用计数变为1
- sp2释放对implementation对象进行管理,其引用计数变为0,该对象被自动删除
boost::shared_ptr的特点:
和前面介绍的boost::scoped_ptr相比,boost::shared_ptr可以共享对象的所有权,因此其使用范围基本上没有什么限制(还是有一些需要遵循的使用规则,下文中介绍),自然也可以使用在stl的容器中。另外它还是线程安全的,这点在多线程程序中也非常重要。
boost::shared_ptr的使用规则:
boost::shared_ptr并不是绝对安全,下面几条规则能使我们更加安全的使用boost::shared_ptr:
- 避免对shared_ptr所管理的对象的直接内存管理操作,以免造成该对象的重释放
- shared_ptr并不能对循环引用的对象内存自动管理(这点是其它各种引用计数管理内存方式的通病)。
- 不要构造一个临时的shared_ptr作为函数的参数。
如下列代码则可能导致内存泄漏:
void test()
{
foo(boost::shared_ptr<implementation>(new implementation()),g());
}
正确的用法为:
void test()
{
boost::shared_ptr<implementation> sp (new implementation());
foo(sp,g());
}
- 智能指针 boost(scoped_ptr,scoped_array,shared_ptr,shared_array) 和 std (auto_ptr)的比较 .
- boost 智能指针auto_ptr scoped_ptr shared_ptr scoped_array shared_array 总结
- 【C++】智能指针的作用,模拟实现auto_ptr,scoped_ptr,shared_ptr,scoped_array,shared_array
- 几种智能指针的比较(std::auto_ptr、boost::scoped_ptr、boost::shared_ptr、boost::weak_ptr)
- C++学习之智能指针--auto_ptr、scoped_ptr、scoped_array、shared_ptr、shared_array、weak_ptr
- c++智能指针:boost::scoped_ptr,boost::shared_ptr,boost::scoped_array
- 智能指针auto_ptr、scoped_ptr、shared_ptr和weak_ptr
- 智能指针scoped_ptr,shared_ptr,weak_ptr和auto_ptr的使用
- std::auto_ptr boost::shared_ptr智能指针的应用
- boost库:智能指针(auto_ptr、scoped_ptr、shared_ptr)浅谈
- auto_ptr scoped_ptr scoped_array shared_ptr weak_ptr
- 智能指针 std::auto_ptr 和 shared_ptr
- 智能指针 std::auto_ptr 和 shared_ptr
- 智能指针 std::auto_ptr 和 shared_ptr
- 智能指针的模拟实现 auto_ptr scoped_ptr shared_ptr
- 智能指针:模拟实现auto_ptr,scoped_ptr,shared_ptr
- 智能指针:auto_ptr 和 scoped_ptr
- Boost智能指针——scoped_ptr和shared_ptr
- 常用存储过程集锦
- 培养创造性思维的20个技巧
- QNX 上的多线程编程
- hdu1787欧拉函数(计算单个欧拉函数值模板)
- 成大事必备9种能力、9种手段、9种心态
- 智能指针 boost(scoped_ptr,scoped_array,shared_ptr,shared_array) 和 std (auto_ptr)的比较 .
- 图像配准----SIFT
- Linux的配置文件
- PO BO VO DTO POJO DAO概念及其作用(附转换图)
- JAVA XML教程
- Boost智能指针——weak_ptr .
- build.xml文档模板
- oracle存储过程代码实例一
- 二分图带权匹配问题