Smart pointers in C + +

Source: Internet
Author: User

Brief introduction

In order to solve the problem of memory leak caused by C + + program, C++11 provides 3 kinds of smart pointers: Std::unique_prt, Std::shared_ptr, Std::weak_ptr. The principle of smart pointers is to store a well-applied memory address inside the smart pointer structure, and then save the smart pointer on the stack. When the smart pointer is out of scope, because the variables on the stack are automatically destroyed, the destructor that was previously passed into the memory block stored inside the smart pointer is also called accordingly. Smart pointers and normal pointers are best not to be used together, otherwise it is easy to use the same object to initialize multiple smart pointers, causing the object to be refactored multiple times. Only the first smart pointer is initialized with a normal pointer, and subsequent smart pointers related to this normal pointer should be initialized with smart pointers.

You need include<memory> when you use it, and you should add-std=c++0x to the compilation.

Unique_ptr

Unique_ptr is the exclusive ownership of the pointer. As long as it is used in the correct way, there will not be a situation where the same pointer shares an object. When using Unique_ptr, you should pay attention to the following items:

<1> unique_ptr cannot copy construction, assignment operation, only through the move function to transfer ownership.

<2> unique_ptr can be compared directly to NULL to determine if the object pointed to by the smart pointer is empty

<3> unique_ptr using the release function simply discards ownership, and the object is not refactored.

<4> because UNIQUE_PTR does not support copying, it cannot be a container object.

    

Output:

    

  

shared_ptr

The difference between shared_ptr and unique_ptr is that you can have multiple shared_ptr pointing to an object at the same time. SHARED_PTR uses a reference count to save how many smart pointers are currently referenced by this object. When the reference count is reduced to 0 o'clock, the object is destroyed.

The following issues should be noted with shared_ptr:

<1> shared_ptr not only can pass in new memory to construct, but also can pass in Unique_ptr, weak_ptr to construct

<2> can use the member function Use_count to get the reference count of an object.

<3> can be compared directly to NULL to determine if the object pointed to by the smart pointer is empty

<4> shared_ptr Internal reference counts are thread-safe, but locking is still required when reading data using the shared_ptr pointer.

<4> note the issue of ring references

    

Output:

    

Weak_ptr

Weak_ptr is mainly used to assist shared_ptr normal operation of the proposed, mainly used to solve the shared_ptr ring reference problem. It is used to refer to an object without increasing the object's reference count. SHARED_PTR can be assigned directly to WEAK_PTR,WEAK_PTR using the LOCL function to obtain shared_ptr.

SHARED_PTR Ring Reference: For example, there are two objects A and B, in a through shared_ptr reference B, in B by shard_ptr reference A, and A and B itself also use shared_ptr reference. Then the reference count of A and B will never be dropped to 0,a and B and will never be released, thus forming a deadlock. For example, the following code, A and B formed a deadlock, and finally who did not release:

    

Output:

    

Use of Enable_shared_from_this

To sum up, enable_shared_from_this is used to let class objects get their own shared_ptr pointers inside their member functions. Like the following code, I want to get my shared_ptr pointer in the fun function:

  

Output:

  

You can see that the object was refactored 2 times. In the fun member function, the shared_ptr p is initialized with the this pointer, which is refactored once the fun function is out. Because the TMP object itself is maintained through shared_ptr, the main function is then refactored once. The root of the problem is that 2 smart pointers are initialized with the this pointer, which is highlighted in the introduction.

If you change the code so that it inherits and Enable_shared_from_this, and then uses Share_from_this to get its own shared_ptr, the problem does not exist.

  

Output:

  

  

Smart pointers in C + +

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.