memory动态内存管理学习之weak_ptr

此头文件是动态内存管理库的一部分。std::weak_ptr 是一种智能指针,它持有对被 std::shared_ptr 管理的对象的非拥有性(“弱”)引用。在访问所引用的对象前必须先转换为 std::shared_ptrstd::weak_ptr 用来表达临时所有权的概念:当某个对象只有存在时才需要被访问,而且随时可能被他人删除时,可以使用 std::weak_ptr 来跟踪该对象。需要获得临时所有权时,则将其转换为 std::shared_ptr,此时如果原来的 std::shared_ptr 被销毁,则该对象的生命期将被延长至这个临时的 std::shared_ptr 同样被销毁为止。std::weak_ptr 的另一用法是打断 std::shared_ptr 所管理的对象组成的环状引用。若这种环被孤立(例如无指向环中的外部共享指针),则 shared_ptr 引用计数无法抵达零,而内存被泄露。可通过令环中的指针之一为弱指针来避免这种情况。

成员函数

(构造函数)

构造新的 weak_ptr
(公开成员函数)

(析构函数)

销毁 weak_ptr
(公开成员函数)

operator=

weak_ptr 赋值
(公开成员函数)
修改器

reset

释放被管理对象的所有权
(公开成员函数)

swap

交换被管理对象
(公开成员函数)
观察器

use_count

返回管理该对象的 shared_ptr 对象数量
(公开成员函数)

expired

检查被引用的对象是否已删除
(公开成员函数)

lock

创建管理被引用的对象的 shared_ptr
(公开成员函数)

owner_before

提供弱指针的基于拥有者的排序
(公开成员函数)

示例代码:

#include <iostream>
#include <memory>

struct C3 { int* data; };

int main()
{
	// weak_ptr constructor example
	std::shared_ptr<int> sp(new int);

	std::weak_ptr<int> wp0;
	std::weak_ptr<int> wp1(wp0);
	std::weak_ptr<int> wp2(sp);
	// weak_ptr use_count example
	std::cout << "use_count:\n";
	std::cout << "wp0: " << wp0.use_count() << '\t';
	std::cout << "wp1: " << wp1.use_count() << '\t';
	std::cout << "wp2: " << wp2.use_count() << '\n';

	// weak_ptr::operator= example
	std::shared_ptr<int> sp1, sp2;
	std::weak_ptr<int> wp;
	// sharing group:
	// --------------
	sp1 = std::make_shared<int>(10);		// sp1
	wp = sp1;								// sp1, wp
	sp2 = wp.lock();						// sp1, wp, sp2
	sp1.reset();							// wp, sp2
	sp1 = wp.lock();						// sp1, wp, sp2
	std::cout << "*sp1: " << *sp1 << '\t';
	std::cout << "*sp2: " << *sp2 << '\n';

	// weak_ptr::reset example
	std::shared_ptr<int> sp3(new int(10));
	std::weak_ptr<int> wp3(sp3);
	std::cout << "1. wp3 " << (wp3.expired() ? "is" : "is not") << " expired\n";
	wp3.reset();
	std::cout << "2. wp3 " << (wp3.expired() ? "is" : "is not") << " expired\n";

	// weak_ptr::swap example
	std::shared_ptr<int> sp4(new int(10));
	std::shared_ptr<int> sp5(new int(20));

	std::weak_ptr<int> wp4(sp4);
	std::weak_ptr<int> wp5(sp5);
	wp4.swap(wp5);
	std::cout << "sp4 -> " << *sp4 << '\t';
	std::cout << "sp5 -> " << *sp5 << '\n';
	std::cout << "wp4 -> " << *wp4.lock() << '\t';
	std::cout << "wp5 -> " << *wp5.lock() << '\n';

	// weak_ptr::expired example
	std::shared_ptr<int> shared(new int(10));
	std::weak_ptr<int> weak(shared);
	std::cout << "1. weak " << (weak.expired() ? "is" : "is not") << " expired\n";
	shared.reset();
	std::cout << "2. weak " << (weak.expired() ? "is" : "is not") << " expired\n";

	// weak_ptr::lock example
	std::shared_ptr<int> sp6, sp7;
	std::weak_ptr<int> wp6;
	// sharing group:
	// --------------
	sp6 = std::make_shared<int>(20);     // sp6
	wp6 = sp6;                            // sp6, wp6
	sp7 = wp6.lock();                     // sp6, wp6, sp7
	sp6.reset();                         //      wp6, sp7
	sp6 = wp6.lock();                     // sp6, wp6, sp7
	std::cout << "*sp6: " << *sp6 << '\n';
	std::cout << "*sp7: " << *sp7 << '\n';

	// weak_ptr::owner_before
	int *p8 = new int(10);
	std::shared_ptr<int> a(new int(20));
	std::shared_ptr<int> b(a, p8);  // alias constructor
	std::weak_ptr<int> c(b);
	std::cout << "comparing a and c...\n" << std::boolalpha;
	std::cout << "value-based: " << (!(a < c.lock()) && !(c.lock() < a)) << '\n';
	std::cout << "owner-based: " << (!a.owner_before(c) && !c.owner_before(a)) << '\n';

	delete p8;
    return 0;
}

运行效果:

参考:

https://cplusplus.com/reference/memory/weak_ptr/

std::weak_ptr - cppreference.com

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值