Unique PTR intelligent pointer
Introduction:
As a kind of intelligent pointer, the key purpose of being designed is to solve the problem of insufficient security of the original pointer
Declaration syntax:
STD:: unique ﹤ PTR < type > variable name {initialization value};
Note: it's better to initialize unique PTR with STD:: make unique
For example:
The first is:
// Initializes a data type space with the initialized value in parentheses std::unique_ptr<int> upa{ std::make_unique<int>(150) }; std::cout << "upa = " << *upa << std::endl;
Result:
Second species:
//Initialize many data type spaces. The number of initialization spaces is in parentheses. Here you can put 10 int s std::unique_ptr<int[]> upb{ std::make_unique<int[]>(10) }; upb[0] = 10; upb[1] = 20; //You can understand it at a glance. std::cout << "upb[0] = " << upb[0] << " upb[1] = " << upb[1]<< std::endl;
Result:
Important features:
This pointer has the same uniqueness as its name, that is, more than two unique pointers cannot point to the same memory address.
Here's an example of a mistake:
std::unique_ptr<int> upa{ std::make_unique<int>(20) }; //First unique pointer std::unique_ptr<int> upb{ upa }; //When initializing, it points to the first one, directly reports an error, and fails to compile
Common methods:
1,get()
Introduction: return to unique's native pointer! How to understand? See below:
std::unique_ptr<int> upa{ std::make_unique<int>(20) }; int* pa = upa.get(); //Define a native pointer to receive it std::cout << "pa=" << *pa << std::endl;
Result:
2,reset()
Introduction: freeing the memory space of unique pointer and pointing the pointer to nullptr(c++11 and above)
For example:
std::unique_ptr<int> upa{ std::make_unique<int>(20) }; upa.reset(); //Free memory and point to nullptr std::cout << upa << std::endl;
Result:
3,release()
Introduce: return the memory address that the original unique pointer points to, but point to null, how to understand?
For example:
std::unique_ptr<int> upa{ std::make_unique<int>(20) }; std::cout << "primary upa address=" << upa << std::endl; int * a = upa.release(); std::cout << "a address="<< a << std::endl; std::cout << "new upa address=" << upa << std::endl;
Result:
4,std::move(unique_ptr)
Introduction: features that cannot be transferred to another unique in order to solve uniqueness
For example:
std::unique_ptr<int> upa{ std::make_unique<int>(20) }; std::unique_ptr<int> upb{}; std::cout << "primary upa==" << upa << std::endl; upb = std::move(upa); std::cout << "upb==" << upb << std::endl; std::cout << "new upa==" << upa << std::endl;
Result:
Note: it is not a class method, but a function in std standard library. Unlike get, it is directly transferred to a unique pointer of Li Gang, while get is transferred to a native pointer!
Sharedx? PTR smart pointer
Introduction:
Unlike unique, it can define many shared pointers pointing to the same address; where is intelligence? Only when the last shared pointer is released can this memory be released! And how many smart pointer calls are recorded at the current address
Declaration syntax:
STD:: sharedx ﹤ PTR < type > variable name {initialization value};
Note: it is better to initialize the shared PTR with STD:: make ﹣ shared
For example:
//First, it's the same form as unique, just changing unique to shared std::shared_ptr<int> spa{ std::make_shared<int>(50) }; //The second method doesn't work. Replace it with the third one //std::shared_ptr<int[]> spb{ std::make_shared<int[]>(4) }; //The third way / / use new to apply for array substitution std::shared_ptr<int[]> spc{ new int[4]{1,2,3,4} };
Common methods:
1,use_count()
Introduction: return how many shared ﹐ PTR calls the current address has
Code:
std::shared_ptr<int> spa{ std::make_shared<int>(50) }; std::cout << spa.use_count() << std::endl; //1 std::shared_ptr<int> spb{ spa }; std::cout << spa.use_count() << std::endl; //2 std::shared_ptr<int> spc{ spa }; std::cout << spa.use_count() << std::endl; //3
Result:
2,unique()
Introduction:
Prototype: bool std::shared_ptr.unique();
Note: if the shared ﹣ PTR is the only shared pointer to the region, return true, otherwise return false
Code:
std::shared_ptr<int> spa{ std::make_shared<int>(50) }; std::cout << spa.unique() << " "; //Only std::shared_ptr<int> spb{ spa }; std::cout << spa.unique() << " "; //Not unique std::shared_ptr<int> spc{ spa }; std::cout << spa.unique() << " "; //Not unique
Result:
3,reset()
Introduction: set the current shared pointer to nullptr. If it is the last one, the memory will be released
Code:
std::shared_ptr<int> spa{ std::make_shared<int>(50) }; std::shared_ptr<int> spb{ spa }; std::shared_ptr<int> spc{ spa }; spa.reset(); std::cout << spb << " "; //Memory not released yet spb.reset(); std::cout << spc << " ";//Memory not released yet spc.reset(); std::cout << spc << " ";//Free memory
Result:
Thanks again for your hard work!! Old fellow iron felt good! I'm potato sailing, thank you!