C + + Smart pointers

Source: Internet
Author: User

C + + smart pointer is mainly on the basis of ordinary pointers encapsulated a layer, so that users of the use of pointers more convenient and rest assured that in the process of use without worrying about the pointer because of the problem caused by the exception. In c++11, the smart pointer has three kinds of:shared_ptr<t> ptr, unique_ptr<t> ptr, weak_ptr<t> ptr;the initialization of shared_ptr<t> PTR can be done in the following ways:1) shared_ptr<t> ptr = make_shared<t> (args); The parameter form of//args is the same as the constructor function of T. 2) shared_ptr<t> ptr (q);     Q can be a smart pointer or a normal pointer (converted to t*, such as &v), but also a new allocated memory. 3) shared_ptr<t> ptr = q;     Q can be either a smart pointer or a generic pointer, but not a newly allocated memory. 4) shared_ptr<t> ptr (U);     U is a unique_ptr, at which point you are placed null,ptr the object that you pointed to before U. 5) shared_ptr<t> ptr (q, D);     Similar to Method 2), but the new delete function D replaces the delete but the smart pointer is best not to mix with different pointers.     Some other operations, such as: 1) ptr = q; PTR points to the object that Q points to, and Q must be a smart pointer. The template type for PTR and Q does not have to be exactly the same, as long as you can move from the template type of Q to the template type of PTR.    At this point PTR originally points to the object reference count minus 1,q to the object reference count plus 1.    2) Ptr.unique ();//Returns whether the number of object references that PTR points to is 1. 3)P.use_count ();//The latter returns the number of references to the object that PTR points to. shared_ptr can be used primarily to prevent memory leaks and hanging pointers when compared to generic pointers:1) Memory leak refers to the dynamic allocation of memory is forgotten released, resulting in this block of memory can not be recycled, it is generally more difficult to detect, unless there is a lot of leaking memory to cause the program memory is not enough. If you use a normal pointer, you can cause this problem, such as: t *p = new T (); T *p= new T (); The memory block that the P points to for the first time is leaked. There is also a situation that can cause a memory leak:t* F (T Arg)           {t *ptr = new T (ARG);              ...;}//At this point, if PTR does not actively release the memory that he points to, wait for the category of function f, there will be a memory leak in the space that PTR points to. and the smart pointer itself is an object, it has its own destructor, when the smart pointer fails, it will automatically call the destructor, delete itself, and the reference value of the object is reduced by 1, the reference value of 0 will delete the pointing object. 2) The hanging pointer is pointing to a piece of memory space, the memory space before saving an object, but later because the delete other pointers were deleted when the pointer is hanging pointer. With a smart pointer, when a smart pointer is deleted, the reference value to the object is first reduced by 1, and the object is deleted if the reference value is 0 at this time. Ptr.reset ();//means that PTR no longer points to the object previously pointed to. Ptr.reset (q);//means that PTR does not point to the object pointed to before, and instead points to the space that Q corresponds to or points to. Ptr.reset (q, D);//Ibid., just replaces the delete function with D, and, if necessary, calls D to delete the object pointed to before. There are a few things to keep in mind when using smart pointers:    1) do not initialize or reset multiple smart pointers with different pointers.     2) do not delete the pointer returned from Get ().     3) do not use the Get () returned pointer to initialize other smart pointers.     4) if get () is used, it is important to note that when a smart pointer deletes the indicated space content, it causes the object pointer returned by get () to be a hanging pointer.     unique_ptr<t> PTR is a guarantee that only one unique_ptr pointer points to an object, and its initialization method is as long as there are several:    1) unique_ptr<t > ptr (q); Q can be a new allocated memory or a normal pointer.     2) unique_ptr<t, d> ptr = ...; Unlike shared_ptr, Unique_ptr needs to specify a pointer to the type of the custom delete function.     Ptr.release (); At this point, PTR no longer points to the object it was pointing to, and returns a pointer to the object that was previously pointed to, it is best to (must) save the return value of the function to a pointer when calling this function, otherwise the memory leaks.     shared_ptr and unique_ptr several identical functions:    1) p; Use p as the condition to determine if the pointer p is empty.     2) *p; Takes the object reference that P points to.     3) p->mem; Accesses the member variable of the object referred to by P.     4) p.get (); Returns a normal pointer to the object being referred to.     WEAK_PTR As the name implies, a weak pointer, binding a shared_ptr to weak_ptr does not affect the number of references to the object that shared_ptr points to. He mainly has the following methods:    1) weak_ptr<t> w = SP; and w = SP; Binds a share_ptr to weak_ptr.     2) weak_ptr<t> W (sp); Ditto.     3) reset (); Release bindings for sp;    4) use_count (); The number of references to the object being referred to.     5) expired (); Determine if Use_count () is 0. 0 returns True, otherwise false is returned.     6) lock (); Returns a shared_ptr that is bound and returns an empty shared_ptr if it does not already exist.

C + + smart pointers

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.