Chapter 12. Dynamic Memory

新标准库提供了两种智能指针(smart pointer)类型来管理动态对象。

智能指针的行为类似常规指针,重要的区别是它负责自动释放所指向的对象。


新标准库提供的这两种智能指针的区别在于管理底层指针的方式:

shared_ptr 允许多个指针指向同一个对象;

unique_ptr 则“独占”所指向的对象。

weak_ptr 伴随类,它是一种弱引用,指向shared_ptr所管理的对象。

这三种类型都定义在 memory 头文件中。


	shared_ptr<string>p1;    //shared_ptr ,可以指向string
	shared_ptr<list<int>>p2; //shared_ptr ,可以指向int 的 list

	if (p1&&p1->empty())     //如果p1不为空,检查它是否指向一个空string
		*p1 = "Hi";          //如果p1指向一个空string,解引用p1,将一个新值赋予string
默认初始化的智能指针中保存着一个空指针。

	p         //Use p as a condition;true if p points to an object.
	*p       //Dereference p to get the object to which p points.
	p.get() //Return the pointer in p.Use with caution;the object to which the returned pointer points
		   //will disappear when the smart pointer deletes it.


shared_ptr 独有的操作:

make_shared<T>(args)  返回一个shared_ptr,指向一个动态分配的类型为T的对象。使用args初始化对象。

Return a shared_ptr pointing to a dynamically allocated object of type T.Use args to initialize that object.


shared_ptr<T>p(q     pshared_ptr q 的拷贝;此操作会递增q中的计数器。q中的指针必须能转换为T*.


p.unique()    Reurns ture if p.use_count() is one;false otherwise.

	shared_ptr<int> p3 = make_shared<int>(42);      // shared_ptr that points to an int with value 42
	
	shared_ptr<string> p4 = make_shared<string>(10, '9');  // p4 points to a string with value 9999999999

	shared_ptr<int> p5 = make_shared<int>();   // p5 points to an int that is value initialized to 0

当然,我们通常用 auto 定义一个对象来保存 make_shared 的结果,这种结果较为简单:

// p6 points to a dynamically allocated, empty vector<string>
auto p6 = make_shared<vector<string>>();
auto p = make_shared<int>(42); // object to which p points has one user
auto q(p); // p and q point to the same object
// object to which p and q point has two users
When we copy or assign a shared_ptr , each shared_ptr keeps track of how many other shared_ptr s point to the same object.

auto p = make_shared<int>(42); // object to which p points has one user
auto q(p); // p and q point to the same object
// object to which p and q point has two users
一旦一个 shared_ptr 的计数器变为0,它就会自动释放自己所管理的对象。

auto r = make_shared<int>(42); // int to which r points has one user
r = q; // assign to r, making it point to a different address
// increase the use count for the object to which q points
// reduce the use count of the object to which r had pointed
// the object r had pointed to has no users; that object is automatically
freed

Programs tend to use dynamic memory for one of three purposes:
1. They don’t know how many objects they’ll need.程序不知道自己需要使用多少对象。
2. They don’t know the precise type of the objects they need.程序不知道所需对象的准确类型。
3. They want to share data between several objects.程序需要在多个对象间共享数据。

	vector<string> v1; // empty vector空vector
	{ // new scope新作用域
		vector<string> v2 = { "a", "an", "the" };
		v1 = v2; // copies the elements from v2 into v1
	} // v2 is destroyed, which destroys the elements in v2,,v2被销毁,其中的元素也被销毁
	  // v1 has three elements, which are copies of the ones originally in v2,v1有三个元素,是原来v2中元素的拷贝









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值