首页 技术 正文
技术 2022年11月13日
0 收藏 775 点赞 4,906 浏览 2398 个字

【1】boost::shared_ptr简介

boost::shared_ptr属于boost库,定义在namespace boost中,包含头文件#include<boost/shared_ptr.hpp>便可以使用。

上篇《智能指针boost::scoped_ptr》中我们看到boost::scoped_ptr独享所有权,不允许赋值、拷贝。

而boost::shared_ptr是专门用于共享所有权的,由于要共享所有权,其在内部使用了引用计数机制。同时也就意味着支持赋值和拷贝。

boost::shared_ptr也是用于管理单个堆内存对象的。

【2】boost::shared_ptr详解

应用实例代码如下:

 #include <iostream>
#include <boost/shared_ptr.hpp> class Int
{
public:
Int(int nValue = )
{
m_nValue = nValue;
std::cout << "Constructor: " << m_nValue << std::endl;
}
~Int()
{
std::cout << "Destructor: " << m_nValue << std::endl;
}
void PrintValue()
{
std::cout << "PrintValue: " <<m_nValue<< std::endl;
}
void SetValue(int nSetValue)
{
m_nValue = nSetValue;
} private:
int m_nValue;
}; void TestShared_Ptr(boost::shared_ptr<Int> spInt)
{ // 注意:无需使用 reference (或 const reference)
spInt->PrintValue();
std::cout << "TestShared_Ptr UseCount: " << spInt.use_count() << std::endl;
} void TestShared_Ptr2()
{
boost::shared_ptr<Int> spInt(new Int());
if (spInt.get())
{
spInt->PrintValue();
spInt.get()->SetValue();
spInt->PrintValue();
(*spInt).SetValue();
spInt->PrintValue();
} std::cout << "TestShared_Ptr2 UseCount: " << spInt.use_count() << std::endl;
TestShared_Ptr(spInt);
std::cout << "TestShared_Ptr2 UseCount: " << spInt.use_count() << std::endl; //spInt.release();// 编译 error: 同样,shared_ptr也没有release函数
} //执行结果如下:
/*
Constructor: 10
PrintValue: 10
PrintValue: 20
PrintValue: 30
TestShared_Ptr2 UseCount: 1
PrintValue: 30
TestShared_Ptr UseCount: 2
TestShared_Ptr2 UseCount: 1
Destructor: 30
*/

实例可见,boost::shared_ptr也可以很方便的使用。并且没有release()函数。

关键的一点,boost::shared_ptr内部维护了一个引用计数,由此可以支持复制、参数传递等。

boost::shared_ptr提供了一个函数use_count(),此函数返回 boost::shared_ptr内部的引用计数。

查看执行结果,我们可以看到在 TestShared_Ptr2函数中,引用计数为1,传递参数后(此处进行了一次复制),

在函数TestShared_Ptr内部,引用计数为2,在TestShared_Ptr返回后,引用计数又降低为1。

另外,由TestShared_Ptr2内创建智能指针对象,到函数结束再析构智能指针对象,确保释放掉内存资源。

当我们需要使用一个共享对象的时候,boost::shared_ptr是最佳选择。

此例也正体现了boost::shared_ptr是支持值语义,提供引用计数机制及RAII支持的智能指针。

【3】boost::shared_ptr总结

boost::shared_ptr的管理机制其实并不复杂,就是对所管理的对象进行了引用计数。

当新增一个boost::shared_ptr对该对象进行管理时,就将该对象的引用计数加一;

减少一个boost::shared_ptr对该对象进行管理时,就将该对象的引用计数减一;

如果该对象的引用计数为0的时候,说明没有任何指针对其管理,才调用delete释放其所占的内存。

boost::shared_ptr并不是绝对安全,下面几条规则能使我们更加安全的使用boost::shared_ptr:

1.避免对shared_ptr所管理的对象的直接内存管理操作,以免造成该对象的重释放

2.shared_ptr并不能对循环引用的对象内存自动管理(这点是其它各种引用计数管理内存方式的通病)。

3.不要构造一个临时的shared_ptr作为函数的参数。

如下列代码则可能导致内存泄漏:

 void test()
{
fun(boost::shared_ptr<Int>(new Int()).g());
}
//正确的用法为:
void test()
{
boost::shared_ptr<Int> spInt(new Int());
fun(spInt.g());
}

当函数g()抛异常的时候就会泄露了,这个是boost文档上特地注明的标准bad Practices。

Good Good Study, Day Day Up.

顺序  选择  循环  总结

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,967
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,487
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,332
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,115
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,748
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,783