C++ The smart pointer in is used to manage external resources , As on a pile new Resources for , When the program hasn't been executed yet delete operation , Function encountered an exception , No resources have been released , Cause memory leak      
The code is as follows :
void fun() { A *ptr = new A(); /*.. ...*/ // The program may throw an exception in this line of code , cause ptr The resource on the pointed heap was not released
delete ptr; }
How does smart pointer solve this problem

Smart pointer is also a class in implementation , There is a constructor , Destructor , Before the end of the function Class automatically calls the destructor , Destructors release resources , So when an exception is encountered , Resources will be released correctly .

Let's talk about it auto_ptr,unique_ptr, share_ptr,weak_ptr Four kinds of intelligent pointer

auto_ptr 

auto_ptr It's a weak intelligence pointer   A resource can only be used by one auto_ptr have , Ownership is transferred when the smart pointer is assigned , The code is as follows

Because there will be a transfer of ownership auto_ptr Can't be used in STL In standard container
void Fun(auto_ptr<A> &ptr) { cout << ptr -> a << endl; cout << "Fun faction
end" << endl; } //auto_ptr void Test() { auto_ptr<A> aptr(new A(5));
auto_ptr<A> bptr; bptr = aptr; // Assignment operator overloaded function auto_ptr<A> cptr(bptr); // copy constructor
Fun(cptr); // Will lead to aptr invalid , take aptr Ownership of resources has shifted ptr Smart pointer cout << cptr -> a << endl; }
 

share_ptr:

share_ptr  Resources can be shared by multiple pointers ,share_ptr Using reference count to manage resources , Cannot be called release(),
call reset() Function current share_ptr Will release ownership of resources , The reference count is subtracted by one , When the reference count is 0 Time , There is no pointer to the resource , Resources will be released ,
Because resources can be pointed to by multiple pointers , So the pointer can be assigned a value , Passing between functions , Stored in STL In the container ,  use_count()
Returns the reference count of the current pointer to the resource (use_count() The efficiency is not high ).

share_ptr Improper use will result in circular references , Resources are not released , Let's not look at this for the time being .

Use below share_ptr Related codes of

Demonstration 1 :
class A{}; shared_ptr<A> aptr(new A()); shared_ptr<A> bptr = aptr; cout <<
aptr.use_count() << endl;
output
A() 2 ~A()
Model 2 :
A *a = new A(); shared_ptr<A> aptr(a); shared_ptr<A> bptr = aptr;
// If you write this way, you will report an error , Causes the same resource on the heap to be delete Twice /*A *a = new A(); shared_ptr<A> aptr(a);
shared_ptr<A> bptr(a);*/
unique_ptr

c++11 Abandoned auto_ptr,unique_ptr Instead auto_ptr, There's only one at a time unique_ptr Points to the given object , The smart pointer is not allowed to use copy constructors
Overloading functions with assignment operators is prohibited  

* Use only move() Function to transfer ownership of an object ,
* reset() Function is used to reassign the resource
* release Release of ownership void Fun(unique_ptr<A>& ptr) // Must be written as a reference { cout<< ptr -> a <<
endl; } //unique_ptr void Test1() { unique_ptr<A> aptr(new A(10));
unique_ptr<A> bptr; // bptr = aptr; //error // unique_ptr<A> cptr(aptr);
//error bptr = move(aptr); cout<< bptr -> a << endl; aptr.reset(new A(20));
cout<< aptr -> a << endl; Fun(aptr); aptr.release(); // cout<< aptr -> a <<
endl; //error cout<< "Test1() end" <<endl; }
 

weak_ptr

weak_ptr Weak intelligent pointer , You can see the reference count of the resource , But I don't use this reference count , use weak_ptr Pointing to a resource does not increase the reference count .

In order to cooperate shared_ptr And the introduction of weak_ptr,weak_ptr It's from share_ptr or
From another weak_ptr Object , Can't point directly to a new one new Resources coming out ,weaker_ptr You can observe the reference of resources , He's not overloaded * and -> operator . 
 use_count() Function returns the reference count of the resource . stay weak_ptr Smart pointer classes can have a member function lcok(); 
The function returns an available from the observed object shared_ptr object , You can assign this object to a new one shred_ptr Smart pointer .

The demonstration code is as follows :
//weak_ptr void Test2() { shared_ptr<A> aptr(new A(10)); shared_ptr<A>
bptr(aptr); weak_ptr<A> wptr(aptr); cout << wptr.use_count() << endl;
shared_ptr<A> cptr = wptr.lock(); // get shared_ptr object cout << cptr -> a << endl;
cout << wptr.use_count() << endl; // Get reference count aptr.reset(new A(20)); // Re point to a new resource
cout << aptr -> a << endl; aptr.reset(); // Point to an empty resource bptr.reset(); cptr.reset();
cout << wptr.use_count() << endl; shared_ptr<A> dptr = wptr.lock();
// When the object reference count on the heap is 0, When they were released ,weak_ptr call lock() Returns a null pointer value (nullptr) cout <<
dptr.use_count() << endl; }
Of all smart pointers, the destructor calls delete, no delete[]; Therefore, when creating an intelligent pointer, it cannot be written as new A[10];
No new An array , An error occurred during resource release

The overall code will be passed later Github upper

The next chapter summarizes the strong intelligence pointer shared_ptr The resulting circular reference ,weak_ptr coordination shared_ptr One of the cases of .

After that, they will Effective C++ The regulations on smart pointer are summarized in the following blog

Technology