C++ smart pointer learning

When using the PCL library for point cloud processing, smart pointers are often used. Smart pointers are relatively common and very important, so be prepared to record them while learning.

C++ does not have a memory recycling mechanism. New objects need to be deleted manually, otherwise memory leaks will occur. C++ introduces smart pointers for dynamic resource management. Use smart pointers to write exception-safe code. Smart pointers can automatically call the object's destructor when the object exits the scope, thereby avoiding memory leaks.

Each shared_ptr object maintains two memory locations internally:
1. A pointer to the object;
2. A pointer used to control reference counting data.
In the process of using smart pointers, circular references need to be avoided. One of the biggest pitfalls of shared_ptr is circular references. Circular references can cause heap memory to not be released correctly.

Compared with ordinary pointers, shared_ptr only provides ->, * and == operators, without operators such as +, -, ++, – and [].
When we create a shared_ptr object without assigning any value, it is empty. When a normal pointer does not allocate space, it is equivalent to a wild pointer, pointing to garbage space, and it is impossible to determine whether it points to useful data.

Do not use the same raw pointer to construct share_ptr, otherwise it will crash.
The normal way to create multiple shared_ptrs is to use an existing shared_ptr to create them, rather than using the same raw pointer.
Example: Top 10 mistakes to avoid when using C++11 smart pointers

int main()

{
    
    

	Aircraft* myAircraft = new Aircraft("F-16");
	
	shared_ptr pAircraft(myAircraft);
	
	cout << pAircraft.use_count() << endl; // ref-count is 1
	
	shared_ptr pAircraft2(myAircraft);
	
	cout << pAircraft2.use_count() << endl; // ref-count is 1
	
	return 0;

}

This will cause ACCESS VIOLATION (Translator's Note: illegal access) and cause the program to crash! ! !

The problem with this is that when the first shared_ptr goes out of scope, the myAircraft object will be destroyed. When the second shared_ptr goes out of scope, the program will try to destroy the already destroyed object again!

Using dynamic lifetime resource classes, the program uses dynamic memory for the following three reasons:
1. The program does not know how many objects (container classes) it needs to use;
2. The program does not know the exact type of objects required;
3. The program needs Share data among multiple objects.

Smart pointers use a technology called RAII (resource acquisition initialization) to encapsulate ordinary pointers, which makes the smart pointer essentially an object and behave like a pointer.

unique_ptr Exclusive Pointer
At any given moment, only one pointer can manage memory. When the pointer goes out of scope, the memory is automatically released.
Pointers of this type cannot be copied and can only be moved.

Three creation methods:
1. Create through an existing raw pointer (raw pointer);
2. Create through new;
3. Create through std::make_unique.
When using method 1, the raw pointer should be assigned to the smart pointer immediately, and the raw pointer should not be called again. After creating the exclusive pointer, you can delete the raw pointer.
When using method 2, two dynamic memory applications will occur, one is the object applied for by new itself, and the other is the resource management object allocation caused by the smart pointer constructor.

unique_ptr can obtain the address through get()
. unique_ptr implements -> and *
1. Member functions can be called through ->;
2. Dereferencing can be dereferenced through asterisks.

Notes on function calls and unique_ptr
1.Passing by value Passing by value
Need to use std::move to transfer memory ownership;
if the parameter is passed directly into the std::make_unique statement, it will automatically be converted to move
2.Passing by reference Passing by reference
If the parameter is set to const Then you cannot change the pointer to
the reset() method to clear the smart pointer.
3.return by value.
Pointing to a local object
can be used as a chain function.

A good video tutorial on smart pointers-Atomic Voice

Guess you like

Origin blog.csdn.net/dyk4ever/article/details/126735220