C++11 smart pointer unique_ptr, shared_ptr and weak_ptr

Source: Internet
Author: User

C++11 smart pointer unique_ptr, shared_ptr and weak_ptr

C++11 unique_ptr, shared_ptr and weak_ptr Smart pointers (smart pointer), defined in <memory>.

Dynamic resources can be managed to ensure that, in any case, the constructed object will eventually be destroyed, i.e. its destructor will eventually be called.

Unique_ptr

UNIQUE_PTR holds exclusive rights to objects, and only one unique_ptr at a time points to a given object (implemented by prohibiting copy semantics, only moving semantics).

Unique_ptr the life cycle of the pointer itself: starts when the unique_ptr pointer is created, until it leaves the scope.

When you leave the scope, if it points to an object, it destroys the object it refers to (by default, the delete operator allows you to specify additional actions).

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<memory>#include<vector>#include<map>voidmytest () {std::unique_ptr<int> Up1 (New int( One));//unique_ptr that cannot be copied//unique_ptr<int> up2 = up1; //err, cannot be compiled byStd::cout << *up1 << Std::endl;// Onestd::unique_ptr<int> UP3 = Std::move (up1);//now P3 is the only unique_ptr of dataStd::cout<< *up3 << Std::endl;// One//std::cout << *up1 << Std::endl; //err, run-time errorUp3.reset ();//explicitly freeing memoryUp1.reset ();//does not cause a run-time error//std::cout << *up3 << Std::endl; //err, run-time errorstd::unique_ptr<int> Up4 (New int( A));//unique_ptr that cannot be copiedUp4.reset (New int( -));//bind dynamic ObjectStd::cout << *up4 << Std::endl;// -Up4= nullptr;//explicitly destroys the object, while the smart pointer changes to a NULL pointer. Equivalent to Up4.reset ()std::unique_ptr<int> Up5 (New int( -)); int*p = Up5.release ();//just release control and not release memoryStd::cout << *p <<Std::endl; //cout << *up5 << Endl;//err, run-time error    DeleteP//free up heap area resources    return;}intMain () {mytest (); System ("Pause"); return 0;}

shared_ptr

SHARED_PTR allows multiple this smart pointer to share the memory of the same heap allocation object, which is implemented by reference counting (reference counting), which records how many shared_ptr together point to an object, once the last such pointer is destroyed, That is, once an object's reference count becomes 0, the object is automatically deleted.

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<memory>#include<vector>#include<map>voidmytest () {std::shared_ptr<int> SP1 (New int( A)); Std::shared_ptr<int> SP2 =SP1; Std::cout<<"cout:"<< sp2.use_count () << Std::endl;//Print Reference countStd::cout<< *SP1 <<Std::endl; Std::cout<< *sp2 <<Std::endl; Sp1.reset (); //Show let reference count minus oneStd::cout <<"Count:"<< sp2.use_count () << Std::endl;//Print Reference countStd::cout<< *sp2 << Std::endl;// A    return;}intMain () {mytest (); System ("Pause"); return 0;}

Weak_ptr

WEAK_PTR is a smart pointer introduced to fit shared_ptr to assist in shared_ptr work, which can be constructed from one shared_ptr or another Weak_ptr object, and its construction and destruction does not cause an increase or decrease in reference count. No overloads * and--but you can use lock to get an available shared_ptr object

The use of weak_ptr is a bit more complicated, it can point to the object memory pointed to by the shared_ptr pointer, but does not own the memory, and with WEAK_PTR member lock, it can return a Share_ptr object pointing to memory, and when the object memory is invalid, Returns the pointer null value NULLPTR.

Note: Weak_ptr does not have ownership of resources, so resources cannot be used directly.
You can construct a shared_ptr from a weak_ptr to take ownership of shared resources.

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<memory>#include<vector>#include<map>voidCheck (std::weak_ptr<int> &WP) {Std::shared_ptr<int> SP = WP.Lock();//Convert to Shared_ptr<int>    if(sp! =nullptr) {Std::cout<<"still:"<< *SP <<Std::endl; }     Else{std::cout<<"still:"<<"pointer is invalid"<<Std::endl; }}voidmytest () {std::shared_ptr<int> SP1 (New int( A)); Std::shared_ptr<int> SP2 =SP1; Std::weak_ptr<int> WP = SP1;//point to the object that shared_ptr<int> refers toStd::cout<<"Count:"<< wp.use_count () << Std::endl;//Count:2Std::cout << *sp1 << Std::endl;// AStd::cout << *sp2 << Std::endl;// ACheck (WP);//still:22Sp1.reset (); Std::cout<<"Count:"<< wp.use_count () << Std::endl;//count:1Std::cout << *sp2 << Std::endl;// ACheck (WP);//still:22Sp2.reset (); Std::cout<<"Count:"<< wp.use_count () << Std::endl;//count:0Check (WP);//Still:pointer is invalid    return;}intMain () {mytest (); System ("Pause"); return 0;}

C++11 smart pointer unique_ptr, shared_ptr and weak_ptr

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.