iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C语言 智能指针 shared_ptr 和 weak_ptr
  • 598
分享到

C语言 智能指针 shared_ptr 和 weak_ptr

2024-04-02 19:04:59 598人浏览 安东尼
摘要

weak_ptr引入可以解决shared_ptr交叉引用时无法释放资源的问题。 示例代码: #include <iOStream> #include <memory

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的文章就介绍到这了,更多相关 shared_ptr 和 weak_ptr内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C语言 智能指针 shared_ptr 和 weak_ptr

本文链接: https://www.lsjlt.com/news/145633.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C语言 智能指针 shared_ptr 和 weak_ptr
    weak_ptr引入可以解决shared_ptr交叉引用时无法释放资源的问题。 示例代码: #include <iostream> #include <memory...
    99+
    2024-04-02
  • C语言智能指针shared_ptr和weak_ptr怎么用
    这篇文章主要讲解了“C语言智能指针shared_ptr和weak_ptr怎么用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C语言智能指针shared_ptr和weak_ptr怎么用”吧!w...
    99+
    2023-06-30
  • 浅析Boost智能指针:scoped_ptr shared_ptr weak_ptr
    一. scoped_ptrboost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放。下列代码演示了该指针的...
    99+
    2022-11-15
    scoped_ptr shared_ptr weak_ptr
  • C++智能指针shared_ptr与weak_ptr的实现分析
    目录shared_ptrweak_ptr内存模型RefCnt 和 Mdel实现shared_ptr 实现weak_ptr 实现shared_from_this()循环引用shared...
    99+
    2024-04-02
  • C语言智能指针之weak_ptr浅析
    目录前言使用环境测试过程现象分析总结前言 weak_ptr这个指针天生一副“小弟”的模样,也是在C++11的时候引入的标准库,它的出现完全是为了弥补它老大shared_ptr天生有缺...
    99+
    2024-04-02
  • C++11中的智能指针shared_ptr、weak_ptr源码解析
    目录1、前言2、源码准备3、智能指针概念4、源码解析4.1、shared_ptr解析4.1.1、shared_ptr4.1.2、__shared_ptr4.1.3、__shared_...
    99+
    2024-04-02
  • C++智能指针shared_ptr
    目录1、什么是shared_ptr?2、shared_ptr支持哪些操作?3、如何创建shared_ptr的实例?4、什么是shared_ptr的引用计数?如何查看?5、shared...
    99+
    2024-04-02
  • C++中Boost的智能指针weak_ptr
    循环引用: 引用计数是一种便利的内存管理机制,但它有一个很大的缺点,那就是不能管理循环引用的对象。一个简单的例子如下: #include <string> #includ...
    99+
    2024-04-02
  • C++中Boost的智能指针shared_ptr
    boost::scoped_ptr虽然简单易用,但它不能共享所有权的特性却大大限制了其使用范围,而boost::shared_ptr可以解决这一局限。顾名思义,boost::shar...
    99+
    2024-04-02
  • C++智能指针之shared_ptr详解
    目录共享指针的初始化方式常用成员函数shared_ptr内存模型make_shared的优缺点优点缺点引用计数比较运算符总结共享指针的初始化方式 1.裸指针直接初始化,但不能通过隐式...
    99+
    2024-04-02
  • C++11智能指针weak_ptr怎么使用
    本篇内容主要讲解“C++11智能指针weak_ptr怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++11智能指针weak_ptr怎么使用”吧!弱指针weak_ptr看起来更像shar...
    99+
    2023-06-19
  • C++中智能指针最常用的shared_ptr和unique_ptr
    目录shared_ptr使用shared_ptr注意unique_ptrshared_ptr 基本用法: 可以通过构造函数, make_shared<T>辅助函数和res...
    99+
    2024-04-02
  • C++特性之智能指针shared_ptr详解
    目录1.创建指针对象2.分离关联的原始指针3.与普通指针比较4.NULL检测shared_ptr 是C++11提供的一种智能指针类,它足够智能,可以在任何地方都不使用时自动删除相关指...
    99+
    2022-12-08
    C++智能指针shared_ptr C++智能指针 C++ shared_ptr
  • C++11智能指针shared_ptr怎么使用
    本篇内容介绍了“C++11智能指针shared_ptr怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!shared_prt的本身是一个...
    99+
    2023-06-19
  • C++智能指针shared_ptr怎么使用
    本篇内容介绍了“C++智能指针shared_ptr怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1、什么是shared_ptr?C+...
    99+
    2023-06-29
  • C++智能指针之shared_ptr如何使用
    这篇文章主要介绍“C++智能指针之shared_ptr如何使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C++智能指针之shared_ptr如何使用”文章能帮助大家解决问题。std::share...
    99+
    2023-06-30
  • C++智能指针之shared_ptr的具体使用
    目录std::shared_ptr概念shared_ptr模板类shared_ptr的构造和析构shared_ptr赋值make_shared计数线程安全?enable_shared...
    99+
    2024-04-02
  • 关于C++智能指针shared_ptr和unique_ptr能否互转问题
    C++中的智能指针最常用的是shared_ptr和unique_ptr,C++新手最常问的问题是我从一个函数中拿到unique_ptr,但要转成shared_ptr才能使用,要怎么...
    99+
    2024-04-02
  • 深入学习C++智能指针之shared_ptr与右值引用的方法
    目录1. 介绍2. 初始化方法2.1 通过构造函数初始化2.2 通过拷贝和移动构造函数初始化2.3 通过 std::make_shared 初始化2.4 通过 reset 方法初始化...
    99+
    2024-04-02
  • C++SmartPointer智能指针详解
    目录一、为啥使用智能指针呢二、shared_ptr智能指针三、unique_ptr智能指针四、weak_ptr智能指针五、智能指针怎么解决交叉引用,造成的内存泄漏5.1 交叉引用的栗...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作