智能指针shared_ptr

shared_ptr的作用有如同指针,但会记录有多少个shared_ptrs共同指向一个对象。这便是所谓的引用计数(reference counting)。一旦最后一个这样的指针被销毁,也就是一旦某个对象的引用计数变为0,这个对象会被自动删除。shared_ptr 在 Technique report I 中已经出现,在effective C++ 中提到过shared_ptr 的使用,现在已经成为C++11 的正式成员。shared_ptr 的自动删除功能是一个很好的防止内存泄露的方法,在C++中也称为RAII(Resource Acquisition Is Initialization)。RAII 的一般做法是这样的:在对象构造时获取资源,接着控制对资源的访问使之在对象的生命周期内始终保持有效,最后在对象析构的时候释放资源。借此,我们实际上把管理一份资源的责任托管给了一个对象。这种做法有两大好处:1,我们不需要显式地释放资源。2,采用这种方式,对象所需的资源在其生命期内始终保持有效 —— 我们可以说,此时这个类维护了一个 invariant。这样,通过该类对象使用资源时,就不必检查资源有效性的问题,可以简化逻辑、提高效率。

下面来看一个简单的例子,涵盖了shared_ptr的基本操作:

#include <memory> 
#include <iostream> 

int main() 
{ 
    auto sp1 = std::make_shared<int>(5);
    std::cout << "sp1.unique() == " << std::boolalpha << sp1.unique() << std::endl; 

    std::shared_ptr<int> sp2 = sp1; 
    std::cout << "sp1.unique() == " << std::boolalpha << sp1.unique() << std::endl;

    std::cout << "sp1.use_count(): "<< sp1.use_count()<<std::endl;
    std::cout << "sp2.use_count(): "<< sp2.use_count()<<std::endl;

    std::cout <<"sp1.get(): "<<sp1.get()<<std::endl;
    std::cout <<"sp2.get(): "<<sp2.get()<<std::endl;

    sp1.reset();

    std::cout << "sp1.use_count(): "<< sp1.use_count()<<std::endl;
    std::cout << "sp2.use_count(): "<< sp2.use_count()<<std::endl;

    std::shared_ptr<int> sp3(new int(10));
    std::cout <<"*sp2: "<< *sp2 <<std::endl;
    std::cout <<"*sp3: "<< *sp3 <<std::endl;    
    sp2.swap(sp3);
    std::cout <<"After swap: "<<std::endl;
    std::cout <<"*sp2: "<< *sp2 <<std::endl;
    std::cout <<"*sp3: "<< *sp3 <<std::endl;    

}

运行结果:

C:\Windows\system32\cmd.exe /c shared_ptr.exe

sp1.unique() == true

sp1.unique() == false

sp1.use_count(): 2

sp2.use_count(): 2

sp1.get(): 001FBEEC

sp2.get(): 001FBEEC

sp1.use_count(): 0

sp2.use_count(): 1

sp2: 5

sp3: 10

After swap:

sp2: 10

sp3: 5

Hit any key to close this window...

说明:unique 函数 判断该指针的引用计数是否为1,use_count() 函数返回该指针的引用计数,get()函数返回该指针的值,reset()清除该指针的引用计数,swap()函数交换两个指针的值。
原文链接: https://www.cnblogs.com/zhuyp1015/archive/2012/08/10/2633065.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/58839

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午8:51
下一篇 2023年2月9日 上午8:52

相关推荐