C ++ Primer shared_ptr and the combined use of new

and the combined use of new shared_ptr
if we do not initialize a smart pointer that is initialized to a null pointer, we can use the pointer returned from new smart pointer is initialized
to accept smart pointer constructor parameter is a pointer to explicit. Therefore, we can not be a pointer implicitly built into a smart pointer, you must use direct initialization form to initialize a smart pointer

shared_ptr<int> p1 = new int(1024);   //错误:必须使用直接初始化形式
shared_ptr<int> p2(new int(1024));    //正确,使用了直接初始化形式

p1 initialization implicitly requires a new returning with int * to create a shared_ptr. Since we can not be built pointer implicitly converted to smart pointers, so this initialization statement is wrong. For the same reason, a function can not return shared_ptr return statement implicit conversion of a normal pointer:

shared_ptr<int> clone(int p){
	return new int(p);   //错误:试图隐式转化为shared_ptr<int>
}

We must shared_ptr explicitly bound to want to return a pointer:

shared_ptr<int> clone(int p){
	//显式用int*创建shared_ptr<int>
	return shared_ptr<int>(new int(p));
}

Other methods to define and change the shared_ptr

shared_ptr<T> p(q);  //p管理内置指针q所指的对象;q必须指向new分配的内存,且能够转化为T*类型
shared_ptr<T> p(u);  //p从unique_ptr u那里接管了对象的所有权,将u置空
shared_ptr<T> p(q, d);   //p将使用可调用对象d来代替delete
p.reset();       //若p是唯一指向其对象的shared_ptr,reset会释放此对象
p.reset(q);      //若传递了可选的参数内置指针q,会令p指向q,否则会将p置空
p.reset(q, d);   

Do not mix ordinary pointers and smart pointers

void process(shared_ptr<int> ptr){
	//使用ptr
}  //ptr离开作用域被销毁
int *x(new int(1024));   //危险:x是一个普通指针,而不是一个智能指针
process(x);    //错误:不能将int*转化为一个shared_ptr<int>
process(shared_ptr<int>(x));   //合法:但内存会被释放
int j = *x;  //未定义的,x为一个空悬指针

The above call, we will pass a temporary shared_ptr to the process. At the end of the invocation expression, the temporary object will be destroyed. Reference count is decremented to zero, at the memory will be freed.

shared_ptr<int> x(new int(1024));
process(x);   //拷贝x会递增它的引用计数,在process中引用计数值为2
int j = *x;   //正确:引用计数值为1

Use a built-pointer to access a smart pointer objects are responsible is very dangerous, because we do not know when an object is destroyed.

Do not get smart pointer initialization or assignment to another smart pointer
smart pointer defines a function called get, and it returns a built-in pointer to the object smart pointer management. This function is designed for the following circumstances: we need to pass to the code can not use a smart pointer built-pointer. Use the pointer to get the return code does not delete this pointer.

shared_ptr<int> p(new int(42));   //引用计数为1
int *q = p.get();  //正确,但不要让q所管理的内存释放
{   //新的程序块
	//未定义:两个独立的shared_ptr指向相同的内存
	shared_ptr<int>(q);
}   //程序块结束,q被销毁,它指向的内存被释放
int foo = *p;    //未定义:P指向的内存被释放了

When we undefined behavior occurs when you use p, p and when destroyed, this memory will be the second time delete
GET pointer is used to deliver access to the code, determine not only if you delete a pointer cases to use get. In particular, never get another smart pointer is initialized or assigned to another pointer pointers.

发布了72 篇原创文章 · 获赞 4 · 访问量 1660

Guess you like

Origin blog.csdn.net/CLZHIT/article/details/104046123