"C + +" smart pointer

Source: Internet
Author: User

Auto_ptr

Auto_ptr is a smart pointer available in the current C + + standard library.

Auto_ptr gets all the go (ownership) of an object at construction time, releasing the object at the time of destruction. We can use AUTO_PTR to improve code security in this way:

int New int (0); Auto_ptr<int> AP (P);

From then on, we don't have to worry about when P should be released, and there is no fear that an exception will have a memory leak, because the Auto_ptr destructor performs the release of the pointer, and the destructor executes after the AP in addition to the scope.

The emergence of auto_ptr is mainly to solve the problem of "resource leakage when thrown by exception". That is, if we let the resource be allocated at the time of the local object construction, it is released when the local object is destroyed, so that if an exception exits during the execution of the function, the local object can be refactored to ensure that the resource is freed.

Here we have only a few points to note:

1) because Auto_ptr will definitely delete the object it owns when it is destructor, two auto_ptr cannot have an object at the same time. Like this:

int New int (0); Auto_ptr<int> ap1 (P); auto_ptr<int> AP2 (P);

Because AP1 and AP2 both think that pointer p is the tube, and when the destruction attempts to delete p, the behavior of deleting the same object two times is undefined in the C + + standard. So we have to prevent this from using AUTO_PTR.

2) Consider the following usage

int New int [];auto_ptr<int> AP (PA);

Because the Auto_ptr destructor removes pointers with delete instead of delete[], we should not use AUTO_PTR to manage an array pointer.

  Unlike reference-counted smart pointers, auto_ptr requires full occupancy of its "bare" pointer. This means that a "bare" pointer cannot be owned by more than two auto_ptr at the same time. So, in copy construction or assignment operations, we have to do special processing to ensure this feature. Auto_ptr's practice is "transfer of ownership", that is, the copy or assignment of the source object loses ownership of the "bare" pointer, so, unlike the generic copy constructor, assignment function, auto_ptr copy constructor, the parameter of the assignment function is a reference instead of a constant reference. Of course, a auto_ptr cannot have more than two "bare" pointers at the same time, so a copy or assignment of the target object will now release the original object.

Here are the points to note:

1) Since a AUTO_PTR has been copied or assigned, it has lost ownership of the original object, and at this point the dereference operation for this auto_ptr is unsafe. As follows:

int New int (0); Auto_ptr<int> ap1 (P); auto_ptr<int> AP2 = ap1; cout// error, at this time AP1 only a null pointer left in the hand

A more hidden scenario is when AUTO_PTR is passed as a function parameter by value, because a local object is generated in the function's scope during the function call to receive the incoming auto_ptr (copy construct), so that the passed-in argument auto_ PTR loses its ownership of the original object, and the original object is deleted locally auto_ptr when the function exits. As follows:

void F (auto_ptr<int> AP) {cout<<*ap}auto_ptr<int> Ap1 (  Newint(0)); F (AP1); cout// error, after the F (AP1) function call, AP1 no longer owns any objects.

2) Because Auto_ptr does not have value semantics, auto_ptr cannot be used in STL standard containers. The so-called value semantics, refers to a type that meets the following criteria (assuming Class A):

A A1; A A2 (A1); A a3;a3=a1;

Then: a2 = = A1, a3==a1

Obviously, Auto_ptr does not meet the above conditions, STL standard containers Use a large number of copy assignment operations, and assume that the type of operation to meet the above conditions.

  

shared_ptr

Auto_ptr cannot be placed in a standard container due to its destructive replication semantics, which cannot meet the requirements of standard containers for elements. The Boost library provides a new kind of smart pointer shared_ptr, which solves the problem of sharing object ownership among multiple pointers, and also satisfies the requirements of the container for elements, so it can be safely placed in the container.

The shared_ptr function is the same as a pointer, but records how many shared_ptr together point to the same object. This is the so-called reference count. Once the last pointer to this is destroyed, that is, once an object's reference count becomes 0, the object is automatically deleted. This is helpful in preventing resource leaks in non-circular data structures.

"C + +" smart pointer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.