In c++98, the smart pointer is implemented by a template "Auto_ptr", Auto_ptr manages the memory allocated by the heap as an object, freeing the acquired memory at the appropriate time (such as destruction). This method of memory management only requires the programmer to use the pointer returned by the new operation as the initial value of the auto_ptr, and the programmer cannot explicitly call delete. such as auto_ptr (new int);
This avoids the problem of the heap memory forgetting to release to some extent. But Auto_ptr also has some drawbacks (the copy returns an Lvalue, cannot call delete[], etc.), so it is discarded in c++11. In c++11 standard photo, unique_ptr,share_ptr, and weak_ptr and other smart pointers are used to automatically reclaim heap allocated objects.
Here is an example of a smart pointer implementation using C++11:
#include <memory>#include<iostream>using namespacestd;intMain () {unique_ptr<int> Up1 (New int( One));//unique_ptr that cannot be copiedunique_ptr<int> up2 = up1;//cannot be compiled bycout<< *up1 << Endl;// Oneunique_ptr Up3= Move (UP1);//now UP3 is data only UNIQUE_PTR only pointercout<< *up3 << Endl;// Onecout << *up1 << Endl;//run-time errorUp3.reset ();//Show free MemoryUp1.reset ();//does not cause a run-time errorcout<< *up3 << Endl;//run-time errorshare_ptr<int> SP1 (New int( A)); Share_ptr<int> SP2 = SP1;//OKcout<< *sp1 << Endl;// Acout << *sp2 << Endl;// ASp1.reset (); cout<< *sp2 << Endl;// A}
The example uses two different smart pointers, unique_ptr and share_ptr, to come from the memory that releases the heap object. The object can be disposed in a destructor or call to reset.
Unique_ptr is tightly bound to the object it refers to, and cannot share the memory of the object with other pointers of the unique_ptr type. Each unique_ptr has only the memory of the object it refers to. However, this ownership can be transferred through the move function of the standard library, such as unique_ptr UP3 = Move (up1), once the transfer is successful, the original unique_ptr loses ownership of the object's memory, then uses the unique_ that has lost power PTR will have a run error.
From the implementation, Unique_ptr is to delete the copy constructor, preserving the package type of the move constructor, the program can only use the right value to construct the Unique_ptr, once the construction is successful, the pointer in the right-value object is invalidated.
Share_ptr is different, it allows multiple this smart pointer to share the memory of the same heap allocation object forever. Unlike Unique_ptr, the reference count is adopted on the implementation, so once a share_ptr loses ownership, the other pointers have no effect, SP1 and SP2 share pointers in the example, and only lower the counter after SP1 reset, without causing the memory to be released. When SP2 reset causes the reference counter to be zero, SHARE_PTR will actually release the occupied memory space.
In the C++11 standard, in addition to Share_ptr and unique_ptr, there are weak_ptr
WEAK_PTR is a smart pointer that does not control the lifetime of an object that points to an shared_ptr-managed object, but does not own the object. Binding a weak_ptr to shared_ptr does not change the shared_ptr reference count. Once the last shared_ptr that points to the object is destroyed, the object is freed, and the object is freed even if weak_ptr points to the object.
When we create a weak_ptr, we use a shared_ptr to initialize it:
Auto p=make_shared_ptr<int>,weak_ptr<int//WP weak share p, The reference count for P has not changed
Since the object may not exist, we cannot directly access the object using WEAK_PTR, and we must call lock (). This function checks whether the object pointed to by weak_ptr exists. With WEAK_PTR member lock, you can return an Share_ptr object in memory and return NULL when the object pointed to is invalid.
#include <memeroy>#include<iostream>using namespacestd;voidCheck (weal_ptr<int> &WP) {Share_ptr<int> SP = WP.Lock();//Convert to Share_ptr<int> if(sp! =nullptr) {cout<<"still"<< *SP <<Endl; } Else{cout<<"pointer is invalid."<<Endl; }}intMain () {share_ptr<int> SP1 (New int( A)); Share_ptr<int> SP2 =SP1; Weal_ptr<int> WP = SP1;//point to the object that share_ptr<int> refers tocout<< *sp1 << Endl;// Acout << *sp2 << Endl;// ACheck (WP);//stillSp1.reset (); cout<< *sp2 << Endl;// ACheck (WP);//stillSp2.reset (); Check (WP); //pointer is invalid. return 0;}
example, SP1 and SP2 are shared objects, and weak_ptr pointer WP also points to that memory, and when both SP1 and SP2 are valid, calling Weak_ptr's Lock function returns an available share_ptr use; When both SP1 and SP2 call reset, Reduces the reference counter of the heap memory to 0, and once the counter is lowered to 0,share_ptr
The memory is freed and invalidated, and the null pointer nullptr is returned when the weak_ptr lock is called.
C++11 's smart pointer