This article mainly introduces the use of smart pointers in boost. (Transferred from: http://www.cnblogs.com/sld666666/archive/2010/12/16/1908265.html)
Memory management is a tedious issue, with two implementations in C + +: Garbage collection mechanism and smart pointers. Garbage collection mechanism because of performance and other reasons are not respected by C + +, and smart pointers are considered to solve the C + + memory problem of the best solution.
1. Definition
A smart pointer is a C + + object that behaves like a pointer, but it can be deleted automatically when it is not needed. Note that this is not a precise definition of "when it is not needed". This is not necessary when you can refer to a lot of aspects: Local variables Exit Function scope, class object is destroyed .... So boost defines a number of different smart pointers to manage different scenarios.
Shared_ptr<t> |
Internally maintains a reference counter to determine if this pointer needs to be freed. Is the most commonly used smart pointer in boost. |
Scoped_ptr<t> |
Automatically released when the scope of the pointer disappears |
Intrusive_ptr<t> |
Also maintains a reference counter, which has better performance than shared_ptr. But ask T to provide this counter yourself. |
Weak_ptr<t> |
Weak pointers, to be used in conjunction with shared_ptr |
Shared_array<t> |
is similar to shared_ptr, but accesses an array |
Scoped_array<t> |
is similar to Scoped_ptr, but accesses 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, releasing the associated resources. It's important to note that SCOPED_PTR cannot share the ownership of a pointer or transfer ownership. This means that the memory address can only be given to the declaration of the variable used, can not be used for other purposes.
Here are a few features of Scoped_ptr:
- The efficiency of the scoped_ptr and the space consumption of the built-in pointers are similar.
- Scoped_ptr cannot be used on containers of standard libraries. (Replace with shared_ptr)
- Scoped_ptr cannot point to an area of memory that can grow dynamically (replaced with Scoped_array)
1 class Test
2 {
3 Public:
4 void print ()
5 {
6 cout << "Test print Now" <<endl;
9 3
8};
9 int _tmain (int argc, _tchar* argv[])
10 {
boost::scoped_ptr<test> x (new test);
X->print ();
return 0;
14}
3.boost::shared_ptr<t>
SHARED_PTR has the following characteristics:
- Maintain a reference counter internally when there is a pointer to this area of memory that is reference count +1, and Vice-1, if there is no pointer to this area, the reference counter is 0 and the memory area is freed.
- Ownership can be shared and transferred.
- Can be used by the container of the standard library
- Cannot point to a dynamically growing memory (replaced with Share_array)
We can 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 ();//reference count is 2
7 ptr_1->print ();//reference count or 2
8 return 0;
7 3
4. boost::intrusive_ptr<t>
Intrusive_ptr's main and share_ptr-like, compared to share_ptr, it is more efficient, but need to maintain a reference counter, here does not do a detailed introduction.
5. boost::weak_ptr<t>
WEAK_PTR is a weak pointer. WEAK_PTR is controlled by shared_ptr, which can be converted to Share_ptr by Share_ptr's constructor or lock member function.
One of the biggest features of WEAK_PTR is that it shares a share_ptr of memory, but neither the construction nor the destruction 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 pointers to this area.
6//Do a lot of other things
7 boost::shared_ptr<test> shareptr_2 = Weakptr.lock ();
8 if (shareptr_2)
9 Shareptr_2->print ();
return 0;
11}
6. Boost::shared_array<t> and Boost::scoped_array<t>
As mentioned earlier, shared_ptr and scoped_ptr cannot be used for arrays of memory (new []), so Shared_array and Scoped_array are their substitutes. We can 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. Several points of note for using smart pointers
Here are a few places to be aware of using smart pointers:
- When you declare a smart pointer, you instantiate it immediately, and you must not release it manually.
- ..._ptr<t> is not a t* type. So:
A: When declaring, ..._ptr<t> not ....._ptr<t*>.
B: You can't assign a t*-type pointer to it.
C: Can not write ptr=null, and use Ptr.reset () instead.
- Cannot loop reference.
- Do not declare a temporary SHARE_PTR, then pass this pointer to a function
8. Summary
Smart pointer use or relatively simple, and can be more effective to solve the problem of C + + memory leaks, you use C + + children's shoes quickly use up.
Smart pointers in boost (RPM)