资源管理方案 RAII--》》智能指针解决方案

在这里插入图片描述

在这里插入图片描述

1.unique_ptr指针的基本使用!

#include <memory>
#include <iostream>
using namespace std;

int main()
{
    // 在这个范围之外,unique_ptr 被释放
    {
        auto i = unique_ptr<int>(new int(10)); // 创建一个 unique_ptr 管理的动态分配内存
        cout << *i << endl;                    // 输出 10
    } // 离开作用域时,unique_ptr `i` 自动释放内存

    // 使用 std::make_unique 进行初始化
    auto w = std::make_unique<int>(10);
    cout << *(w.get()) << endl;                // 输出 10

    // 尝试复制 unique_ptr 会导致编译错误
    // auto w2 = w; // 编译错误:unique_ptr 不支持复制语义

    // unique_ptr 支持移动语义
    auto w2 = std::move(w); // w 的所有权转移给 w2
    cout << ((w.get() != nullptr) ? (*w.get()) : -1) << endl; // 输出 -1,因为 w 已经是 nullptr
    cout << ((w2.get() != nullptr) ? (*w2.get()) : -1) << endl; // 输出 10,w2 现在拥有该内存

    return 0;
}

首先

2.shared_ptr 的基本使用

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

#include <memory>
#include <iostream>

struct B;
struct A {
    std::shared_ptr<B> ptrB;
    ~A() { std::cout << "A destroyed\n"; }
};

struct B {
    std::weak_ptr<A> ptrA;  // 使用 weak_ptr 打破循环引用
    ~B() { std::cout << "B destroyed\n"; }
};

int main() {
    auto a = std::make_shared<A>();
    auto b = std::make_shared<B>();
    a->ptrB = b;            // A 拥有 shared_ptr 指向 B
    b->ptrA = a;            // B 拥有 weak_ptr 指向 A,避免循环引用
    return 0;               // a 和 b 离开作用域时,资源会被正常释放
}

#include <string>
#include <iostream>
#include <memory>
using namespace std;

struct B;
struct A {
	shared_ptr<B> pb;
	~A()
	{
		cout << "~A()" << endl;
	}
};
struct B {
	shared_ptr<A> pa;
	~B()
	{
		cout << "~B()" << endl;
	}
};

// pa 和 pb 存在着循环引用,根据 shared_ptr 引用计数的原理,pa 和 pb 都无法被正常的释放。
// weak_ptr 是为了解决 shared_ptr 双向引用的问题。
struct BW;
struct AW
{
	shared_ptr<BW> pb;
	~AW()
	{
		cout << "~AW()" << endl;
	}
};
struct BW
{
	weak_ptr<AW> pa;
	~BW()
	{
		cout << "~BW()" << endl;
	}
};

void Test()
{
	cout << "Test shared_ptr and shared_ptr:  " << endl;
	shared_ptr<A> tA(new A());                                               // 1
	shared_ptr<B> tB(new B());                                                // 1
	cout << tA.use_count() << endl;
	cout << tB.use_count() << endl;
	tA->pb = tB;
	tB->pa = tA;
	cout << tA.use_count() << endl;                                        // 2
	cout << tB.use_count() << endl;                                        // 2
}
void Test2()
{
	cout << "Test weak_ptr and shared_ptr:  " << endl;
	shared_ptr<AW> tA(new AW());
	shared_ptr<BW> tB(new BW());
	cout << tA.use_count() << endl;                                        // 1
	cout << tB.use_count() << endl;                                        // 1
	tA->pb = tB;
	tB->pa = tA;
	cout << tA.use_count() << endl;                                        // 1
	cout << tB.use_count() << endl;                                        // 2
}

int main()
{
	Test();
	Test2();


	return 0;
}

在这里插入图片描述

下面总结四种指针

在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋到亦天凉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值