61st lesson Smart Pointer class template

Source: Internet
Author: User

1. The meaning of smart pointers

(1) One of the most important class templates in the modern C + + development Library

(2) The main means of automatic memory management in C + +

(3) Ability to avoid memory-related problems (such as memory leaks, multiple releases of memory, etc.) to a large extent

2. Smart Pointers in the STL

(1) Auto_ptr smart pointer

① the memory space to be destroyed when the life cycle ends

② can only be used to manage a single dynamically created object, not to manage dynamically created arrays. That is , you cannot point to a heap array, only the pointer heap object (variable)

int New int [];auto_ptr<int//auto_ptr point to the heap array. When released, the equivalent of delete pn, not delete[], causes a memory leak. 

③ a heap of space belongs to only one smart pointer object, and multiple smart pointer objects cannot point to the same heap space

On the use of "programming experiment" auto_ptr

(2) Other smart pointers

①shared_ptr: With reference counting mechanism to support multiple pointer objects pointing to the same piece of memory

weak_ptr: A smart pointer introduced with shared_ptr to assist with shared_ptr work, which can be constructed from a shared_ptr or another Weak_ptr object to obtain the observed power of the resource. However, weak_ptr cannot directly access resources, and its construction and destruction will not cause an increase or decrease in pointer reference counts .

③unique_ptr: Is the evolutionary version of Auto_ptr, and a pointer object that points to a memory space, but cannot copy constructs and assigns values.

The usage of the "programming Experiment" shared_ptr and weak_ptr and the circular reference problem of reference counting

/*the usage of shared_ptr and weak_ptr and the circular reference problem of reference counting g++ main.cpp-std=c++11*//*Within the man class, reference a man inside a Woman,woman class through the shared_ptr pointer. When a man and a woman are husband and wife, they have a direct problem of mutual reference. Inside man there is a shared_ptr variable for managing the wife lifetime, meaning that wife must die after husband dies. Similarly, there is a shared_ptr variable within the woman that manages the husband lifetime, meaning that husband must die after wife dies.    This is the problem of circular references: The life of the husband is determined by the life of the wife, the life of the wife is determined by the life of the husband, and the last two die, violating the laws of nature, resulting in a memory leak. The key to solve the HARED_PTR circular reference problem is on the weak_ptr hand. The Weak_ptr object references a resource without increasing the reference count, but it can use the lock () method to determine whether the resource it manages is freed. Another very natural question is: since weak_ptr does not increase the reference count of resources, then when using the Weak_ptr object, the resources are suddenly released what to do? Hehe, the answer is that you simply cannot access resources directly through weak_ptr. So how to access resources indirectly through weak_ptr? The answer is that weak_ptr generates a shared_ptr,shared_ptr for you when you need to access the resource to ensure that the resources it manages will not be released until Shared_ptr is released. The way to create shared_ptr is to call the weak_ptr Lock () method. */#include<iostream>#include<memory>//For smart pointerusing namespacestd;classMan ;classwoman{Private: shared_ptr<Man> _husband;//husband must die before wife dies.  Public:    voidSethusband (shared_ptr<man>Mans) {_husband=Man ; }            voidShow () {cout<<"Age =, name = Xishi"<<Endl; }        ~Woman () {cout<<"~woman ()"<<Endl; }};classman{Private: weak_ptr<Woman> _wife;//weak_ptr cannot be used to access resources directly//shared_ptr<woman> _wife;//This sentence represents the life of wife in accordance with man, that is,//A wife must die before her husband dies! This is the problem with circular references Public:    voidSetwife (shared_ptr<woman>woman) {_wife=woman; }        voiddosomething () {//weak_ptr cannot be used to directly access resources, you can create a temporary share_ptr pointer to a resource's ownership through the Lock () method        if(Shared_ptr<woman> SPT = _wife.)Lock())//determine if a resource is released{SPT-Show (); }    }        ~Man () {cout<<"~man"<<Endl; }};intMain () {shared_ptr<Man> m (NewMan ()); shared_ptr<Woman> W (NewWoman ()); if(M &&W) {m-Setwife (W); W-Sethusband (m); } m-dosomething (); return 0;}/*Age =, name = Xishi~woman () ~man*/

3. the smart pointer in Qt

(1) Qpointer

① this pointer is automatically empty when the object it points to is destroyed .

does not automatically destroy objects that you point to when you refactor

③ this pointer is primarily used to solve the problem of multiple releases caused by manually releasing objects. Because when an object is deleted, all qpointer pointers to that object are empty.

(2) Qsharedpointer

Reference count smart pointer

② can be freely copied and assigned to a value

③ when the reference count is 0 o'clock , the object that is pointed to is deleted , and the point is automatically disposed.

Smart pointers in the "programming Experiment" QT

(3) Other smart pointers in QT

①qweakpointer;②qscopedpointer;③qscopedarraypointer;④qshareddatapointer;⑤qexplicitlyshareddatapointer

4. Custom Smart pointer class template

"Programming experiments" creating smart pointer class templates

5. Summary

(1) Smart pointers are the main means of automatic memory management in C + +

(2) Smart pointers have different forms of expression on various platforms

(3) Smart pointers to avoid memory-related problems as much as possible

(4) Support for smart pointers is provided in STL and QT

61st lesson Smart Pointer class template

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.