This articleArticleThis section describes how to use smart pointers in boost.
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. Definition
A smart pointer is 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> |
An internal reference counter is maintained to determine whether the pointer needs to be released. It is the most common smart pointer in boost. |
Scoped_ptr <t> |
The pointer is automatically released when its scope disappears. |
Intrusive_ptr <t> |
It also maintains a reference counter, which has better performance than shared_ptr. However, t is required to provide the counter by itself. |
Weak_ptr <t> |
Weak pointer, used in combination with shared_ptr |
Shared_array <t> |
Similar to shared_ptr, but accessing an array |
Scoped_array <t> |
Similar to scoped_ptr, but accessing an 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 some 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 (instead of scoped_array)
1 Class Test
2 {
3 Public :
4 Void Print ()
5 {
6 Cout < " Test print now " < Endl;
7 }
8 };
9 Int _ Tmain ( Int Argc, _ tchar * Argv [])
10 {
11 Boost: scoped_ptr < Test > X ( New Test );
12 X -> Print ();
13 Return 0 ;
14 }
3. Boost: shared_ptr <t>
Shared_ptr has the following features:
- 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.
- Ownership can be shared and transferred.
- Can be used by containers in the standard library
- It cannot point to a dynamically growing memory (replaced by pai_array)
Let's take a look at the following example:
1 Int _ Tmain ( Int Argc, _ tchar * Argv [])
2 {
3 Boost: shared_ptr < Test > Ptr_1 ( New Test );
4 Ptr_1 -> Print (); // Reference count is 1
5 Boost: shared_ptr < Test > Ptr_2 = Ptr_1;
6 Ptr_2 -> Print (); // The reference count is 2.
7 Ptr_1 -> Print (); // The reference count is still 2.
8 Return 0 ;
9 }
4. Boost: intrusive_ptr <t>
Intrusive_ptr is mainly the same as pai_ptr. Compared with pai_ptr, intrusive_ptr is more efficient. However, you need to maintain a reference counter by yourself, which is not described 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.
1 Int _ Tmain ( Int Argc, _ tchar * Argv [])
2 {
3 Boost: shared_ptr < Test > Shareptr ( New Test );;
4 Boost: weak_ptr < Test > Weakptr (shareptr );
5 // Weakptr is used to save the cursor pointing to the internal storage region.
6 // Doing a lot of other things
7 Boost: shared_ptr < Test > Shareptr_2 = Weakptr. Lock ();
8 If (Shareptr_2)
9 Shareptr_2 -> Print ();
10 Return 0 ;
11 }
6. Boost: shared_array <t> and boost: scoped_array <t>
As mentioned above, shared_ptr and scoped_ptr cannot be used in the Array Memory (new []), so shared_array and scoped_array are their substitutes. Let's take a look at the usage of shared_array.
1 Int _ Tmain ( Int Argc, _ tchar * Argv [])
2 {
3 Const Int Size = 10 ;
4 Boost: shared_array < Test > A ( New Test []);
5 For ( Int I = 0 ; I < Size; ++ I)
6 A [I]. Print ();
7 Return 0 ;
8 }
7. Notes on using smart pointers
The following are some notes for using smart pointers:
- When declaring a smart pointer, you must instantiate it immediately, and you must not manually release it.
- ... _ PTR <t> not T * type. Therefore:
A: When I declare a statement, I need... _ PTR <t> instead ..... _ PTR <t *>
B: You cannot assign a pointer of the T * type 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.
8. Summary
Smart pointers are relatively simple to use and can effectively solve the problem of C ++ Memory leakage. Please use C ++ kids shoes.