温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C语言智能指针shared_ptr和weak_ptr怎么用

发布时间:2022-04-12 10:39:51 来源:亿速云 阅读:163 作者:iii 栏目:开发技术

这篇文章主要讲解了“C语言智能指针shared_ptr和weak_ptr怎么用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C语言智能指针shared_ptr和weak_ptr怎么用”吧!

weak_ptr引入可以解决shared_ptr交叉引用时无法释放资源的问题。

示例代码:

#include <iostream>
#include <memory>

using namespace std;

class B;

class A{
public:
    A(){cout << "A constructor ... "<< endl;}
    ~A(){cout << "A destructor ..." << endl;}
    std::shared_ptr<B> pb;
};

class B{
public:
    B(){cout << "B constructor ... "<< endl;}
    ~B(){cout << "B destructor ..." << endl;}
    std::shared_ptr<A> pa;
};

int main(int argc, char **argv) {
    
    std::shared_ptr<int> a = std::make_shared<int>(3);
    std::shared_ptr<char> b = std::make_shared<char>('a');
    
    std::cout << "shared_ptr object(int) size = " << sizeof(a) << std::endl;
    std::cout << "shared_ptr object(char) size = " << sizeof(b) << std::endl;
    
    std::weak_ptr<A> shadow_a;
    std::weak_ptr<B> shadow_b;
    
    {
    std::shared_ptr<A> ptr_a = std::make_shared<A>();
    std::shared_ptr<B> ptr_b = std::make_shared<B>();
    
    shadow_a = ptr_a;
    shadow_b = ptr_b;
    
    ptr_a->pb = ptr_b;
    ptr_b->pa = ptr_a;
    
    cout << "reference count of A = " << shadow_a.use_count() << endl;
    cout << "reference count of B = " << shadow_b.use_count() << endl;
    cout << endl; 
    }
    
    cout << "reference count of A = " << shadow_a.use_count() << endl;
    cout << "reference count of B = " << shadow_b.use_count() << endl;
    
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

运行代码得到以下输出:

shared_ptr object(int) size = 16
shared_ptr object(char) size = 16
A constructor ... 
B constructor ... 
reference count of A = 2
reference count of B = 2

reference count of A = 1
reference count of B = 1
Hello, world!

从结果可以看出,由于交叉引用导致申请的内存A,B无法正常释放。
为什么会这样呢?这个应该从析构原理进行考虑,shared_ptr引用计数需要为0才会进行析构!但是ptr_a离开作用域会导致A引用计数减少1,但是A的引用计数此时为1,那么 pb不会释放;同理,ptr_b离开作用域会导致B引用计数减少1,但是B的引用计数为此时为1,那么pa不会释放。如此导致了资源无法释放掉。
由于weak_ptr并不会改变shared_ptr的引用计数,所以修改类A,和类B中的shared_ptr对象为weak_ptr对象即可释放资源。

修改后的代码如下:

#include <iostream>
#include <memory>

using namespace std;

class B;

class A{
public:
    A(){cout << "A constructor ... "<< endl;}
    ~A(){cout << "A destructor ..." << endl;}
    //std::shared_ptr<B> pb;
    std::weak_ptr<B> pb;
};

class B{
public:
    B(){cout << "B constructor ... "<< endl;}
    ~B(){cout << "B destructor ..." << endl;}
    //std::shared_ptr<A> pa;
    std::weak_ptr<A> pa;
};

int main(int argc, char **argv) {
    
    std::shared_ptr<int> a = std::make_shared<int>(3);
    std::shared_ptr<char> b = std::make_shared<char>('a');
    
    std::cout << "shared_ptr object(int) size = " << sizeof(a) << std::endl;
    std::cout << "shared_ptr object(char) size = " << sizeof(b) << std::endl;
    
    std::weak_ptr<A> shadow_a;
    std::weak_ptr<B> shadow_b;
    
    {
    std::shared_ptr<A> ptr_a = std::make_shared<A>();
    std::shared_ptr<B> ptr_b = std::make_shared<B>();
    
    shadow_a = ptr_a;
    shadow_b = ptr_b;
    
    ptr_a->pb = ptr_b;
    ptr_b->pa = ptr_a;
    
    cout << "reference count of A = " << shadow_a.use_count() << endl;
    cout << "reference count of B = " << shadow_b.use_count() << endl;
    cout << endl; 
    }
    
    cout << "reference count of A = " << shadow_a.use_count() << endl;
    cout << "reference count of B = " << shadow_b.use_count() << endl;
    
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

运行结果如下,可以正常释放资源。

shared_ptr object(int) size = 16
shared_ptr object(char) size = 16
A constructor ... 
B constructor ... 
reference count of A = 1
reference count of B = 1

B destructor ...
A destructor ...
reference count of A = 0
reference count of B = 0
Hello, world!

感谢各位的阅读,以上就是“C语言智能指针shared_ptr和weak_ptr怎么用”的内容了,经过本文的学习后,相信大家对C语言智能指针shared_ptr和weak_ptr怎么用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI