Explain the Boost smart pointer in C ++

Source: Internet
Author: User

Memory Management is a complicated problem. C ++ has two implementation solutions: Garbage collection mechanism and smart pointer. Because of performance and other reasons, the garbage collection mechanism is not highly praised by C ++, and smart pointers are considered to be the best solution to the C ++ memory problem. 1. Define a smart pointer As a C ++ object. This object acts like a pointer, but it can be automatically deleted when it is not needed. Note that this "when it is not needed" is not a precise definition. When this is not needed, it can be pointed to many aspects: local variables exit the function scope, class objects are destructed ....... So boost defines multiple smart pointers to manage different scenarios. Shared_ptr <T> maintains a reference counter internally to determine whether the pointer needs to be released. It is the most common smart pointer in boost. Scoped_ptr <t> when the scope of the pointer disappears, intrusive_ptr is automatically released <T> and a reference counter is maintained, which has better performance than shared_ptr. However, T is required to provide the counter by itself. Weak_ptr <T> weak pointer. It must be similar to shared_ptr in combination with shared_array <T> and shared_ptr, But It accesses the array scoped_array <T> and scoped_ptr, But It accesses array 2. boost: scoped_ptr <T> scoped_ptr is the simplest smart pointer in boost. The purpose of scoped_ptr is also very simple. When a pointer leaves its scope, it releases related resources. Note that scoped_ptr cannot share pointer ownership or transfer ownership. That is to say, this memory address can only be used for declared variables and cannot be used for other purposes. The following are several features of scoped_ptr: the efficiency of scoped_ptr is similar to that of built-in pointers for space consumption. Scoped_ptr cannot be used on the container of the standard library. (Replaced by shared_ptr) scoped_ptr cannot point to a dynamically growing memory area (replaced by scoped_array) class test {public: void print () {cout <"test print now" <endl ;}}; int _ tmain (int argc, _ TCHAR * argv []) {boost :: scoped_ptr <test> x (new test); x-> print (); return 0;} 3. boost: shared_ptr <T> shared_ptr has the following features: 1. maintain a reference counter internally. When a pointer points to this memory area, the reference count is + 1, and vice versa. If no pointer points to this area, the reference counter is 0, release the memory area. 2. ownership can be shared and transferred. 3. can be used by containers in the standard library. 4. it cannot point to a dynamically growing memory (replaced by javas_array). Let's take a look at the following example: int _ tmain (int argc, _ TCHAR * argv []) {boost :: shared_ptr <test> ptr_1 (new test); ptr_1-> print (); // The reference count is 1 boost: shared_ptr <test> ptr_2 = ptr_1; ptr_2-> print (); // The reference count is 2 ptr_1-> print (); // The reference count is still 2 return 0;} 4. boost: intrusive_ptr <T> the efficiency of intrusive_ptr is the same as that of cmd_ptr. Compared with cmd_ptr, it is more efficient, but you need to maintain a reference counter by yourself. I will not describe it in detail here. 5. Boost: weak_ptr <T> weak_ptr is a weak pointer. Weak_ptr is controlled by shared_ptr. It can be converted to pai_ptr through the constructor of pai_ptr or the lock member function. One of the biggest features of weak_ptr is that it shares the memory of javas_ptr, but neither the construction nor the analysis of a weak_ptr will affect the reference counter. Int _ tmain (int argc, _ TCHAR * argv []) {boost: shared_ptr <test> sharePtr (new test); boost :: weak_ptr <test> weakPtr (sharePtr); // weakPtr is used to save the cursor pointing to the memory region in this region. // It does a lot of other things. boost :: shared_ptr <test> sharePtr_2 = weakPtr. lock (); if (sharePtr_2) sharePtr_2-> print (); return 0;} 6. boost: shared_array <T> and Boost: scoped_array <T> previously mentioned that shared_ptr and scoped_ptr cannot be used in Array Memory (new []). therefore, shared_array and scoped_array are their substitutes. Let's take a look at the usage of shared_array: int _ tmain (int argc, _ TCHAR * argv []) {const int size = 10; boost :: shared_array <test> a (new test []); for (int I = 0; I <size; ++ I). print (); return 0;} 7. note the following points for using smart pointers: 1. when declaring a smart pointer, you must instantiate it immediately, and you must not manually release it.... _ Ptr <T> not T * type. So: a: To declare... _ Ptr <T> instead ..... _ Ptr <T *> B: the pointer of the T * type cannot be assigned to it. c: instead of ptr = NULl, instead of ptr. reset. It cannot be referenced cyclically. Do not declare a temporary cmd_ptr and pass this pointer to a function.

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.