C + + Smart pointer Usage Summary

Source: Internet
Author: User

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. Smart pointer 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.

When the scope of this pointer disappears Automatically releases
also maintains a A reference counter that has better performance than the shared_ptr. But ask T to provide this counter yourself.
weak pointers, to and shared_p TR with
SCOPED_ARRAY<T>
2. Boost::scoped_ptr<t>2.1 Definitions

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.

2.2 Features

(1) Scoped_ptr efficiency and space consumption of the built-in pointer is similar .

(2) Scoped_ptr cannot be used on containers of standard libraries . (Replace with shared_ptr)

(3) Scoped_ptr cannot point to an area of memory that can grow dynamically (replace with Scoped_array)

2.3 Examples
1 classTest2 { 3  Public: 4     voidprint ()5     { 6cout <<"Test Print now"<<Endl;7     } 8 };9 int_tmain (intARGC, _tchar*argv[])Ten {  OneBoost::scoped_ptr<test> x (Newtest);  AX->print (); -     return 0;  -}
View Code3.boost::shared_ptr<t>3.1 Definitions

Boost::shared_ptr is a smart pointer that can share ownership .

3.2 Features

(1) To maintain a reference counter internally, when there is a pointer to this area of memory is reference count +1, and Vice-1, if there is no pointer to this area , the reference counter is 0, frees the memory area .

(2) Ownership can be shared and transferred .

(3) can be used by the container of the standard library

(5) cannot point to a dynamically growing memory (replaced with Share_array)

3.3 Examples
1 int_tmain (intARGC, _tchar*argv[])2 { 3Boost::shared_ptr<test> ptr_1 (Newtest); 4Ptr_1->print ();//reference count is 15Boost::shared_ptr<test> ptr_2 =ptr_1;6Ptr_2->print ();//reference count is 27Ptr_1->print ();//The reference count or the 28     return 0; 9}
View Code4. 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>5.1 Definitions

Weak_ptr is a weak pointer . Weak_ptr is controlled by shared_ptr , which can be converted to share_ptrby Share_ptr's constructor or lock member function .

5.2 Features

(1) One of the biggest features of weak_ptr is that it shares a share_ptr memory

(2) Neither construction nor destruction of a weak_ptr will affect the reference counter .

5.3 Examples
1 int_tmain (intARGC, _tchar*argv[])2 { 3Boost::shared_ptr<test> Shareptr (Newtest);;4Boost::weak_ptr<test>weakptr (shareptr);5     //weakptr is used to save pointers to this area.6     //doing a whole bunch of other things.7Boost::shared_ptr<test> shareptr_2 = weakptr.Lock(); 8     if(shareptr_2)9Shareptr_2->print ();Ten     return 0;  One}
View Code6. Boost::shared_array<t> and boost::scoped_array<t>6.1 Definitions

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.

6.2 Examples
1 int_tmain (intARGC, _tchar*argv[])2 { 3     Const intSize =Ten; 4Boost::shared_array<test> A (Newtest[]);5      for(inti =0; i < size; ++i)6 a[i].print ();7     return 0; 8}
View Code7. Several points of note for using smart pointers

(1) When declaring a smart pointer, instantiate it immediately and must not release it manually .

(2) ..._ptr<t> is not a t* type . So:

A: When declaring, be ..._ptr<t> rather than ....._ptr<t*>.

B: You cannot assign a pointer to a t* type .

C: can not write Ptr=null, and use ptr.reset () instead.

(3) cannot loop reference .

(4) do not declare a temporary share_ptr, and then pass the pointer to a function .

8. Summary

Smart pointers are relatively simple to use, and can be more effective in solving the problem of C + + memory leaks .

Original link: http://www.cnblogs.com/sld666666/archive/2010/12/16/1908265.html

C + + Smart pointer Usage Summary

Related Article

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.