C + + does not provide a garbage collection mechanism, and the smart pointers provided by C++11 can solve the problem of C + + memory leaks to some extent.
C++11 provides a shared smart pointer (shared_ptr), exclusive smart pointer (UNIQUE_PTR), weak reference pointer pointer (weak_ptr), need to reference <memory>
The smart pointer essentially stores the template class of the dynamically allocated (heap) object, which controls the lifetime of the heap object and ensures that dynamically allocated heap objects are automatically destroyed when the pointer scope is left.
Prevent heap memory leaks.
1. Std::shard_ptr is typically used for multiple smart pointers while using the same memory resource, its internal use reference count count, each use it once, the number of internal references increased by 1; On the other hand, the reference count is reduced by 1.
Minus 0 o'clock, the heap memory pointed to is deleted.
1.1 Initialization: Recommended use of make_shared<t>
std::shared_ptr<int> P1 (newint(1)); // constructor initializes auto p2=std::make_shared<int> (1); // make_shared<t> // std::shared_ptr<int> p2=std::make_shared<int> (1); int *pi=newint(1); Std::shard_ptr<int> P3 (PI);
1.2 Using the same method as the normal raw pointer
if (p1) //Determine if shared_ptr is empty { std::cout<<'P1 is not null'<< Endl; cout<<*p1<<Endl; Gets the value that the pointer points to the object
}
int *p=p1.get (); Get raw pointer
One of the biggest pitfalls of 1.3shared_ptr is circular referencing, which causes heap memory to be released incorrectly, causing a memory leak.
The following is a typical instance where the test indicates that the heap object is not disposed, but that the reference count is reduced from 2 to 1
#include <iostream>#include<memory>structA;structB;structa{std::shared_ptr<B>bptr; A () {std::cout<<"A is created"<<Std::endl;} ~a () {std::cout<<"A is deleted!"<<Std::endl;}};structb{std::shared_ptr<A>aptr; B () {std::cout<<"B is created!"<<Std::endl;} ~b () {std::cout<<"B is deleted!"<<Std::endl;}}; Std::weak_ptr<A>WPA;voidtestptr () {std::shared_ptr<A> ap (NewA); Std::shared_ptr<B> BP (NewB); AP->bptr=p; BP->aptr=ap; WPA =ap; Weak_ptr gets the reference count of the current observation resource Std::cout<<wpa.use_count () <<std::endl; Ouput 2}intMain () {testptr (); Std::cout<<wpa.use_count () <<std::endl;//Output 1return 0;}
2.unique_ptr is used to manage resources or manage arrays with only one smart pointer;
It is an exclusive smart pointer that does not permit other smart pointers to share the same block of memory.
The assignment of one unique_ptr to another unique_ptr is not permitted.
transfer ownership of a heap object through the Std::move function
std::unique_ptr<int> Ptri1 (newint(1)); // //Cannot assign value std::unique_ptr<int//transfer ownership ////Loss of ownership //Get ownership
Note: unique_ptr In addition to exclusivity, unique can also point to an array
Std::shared_ptr<int []> PTR3 (new int[10]);
Std::unique_ptr<int []> PTR4 (new int[10]);
ptr4[9]=9;
3.WEAK_PTR: Used to monitor shared_ptr, which does not manage pointers inside the shared_ptr,
There are no overloads * and->, no pointers are shared, and resources cannot be manipulated.
1) Get the reference count of the current observation resource by Use_count (). Example see above
2) Determine if the resource is released through the expired () method
if (wp.expired ())
{
std::cout<< "Not expired!" <<endl;
}
3) Lock () method to get the monitored shared_ptr object
C++11 Smart pointer