35. Smart Pointers
Smart pointers are pointers that own the object they point to and automatically destroy the object they point to and deallocate the memory once the pointers go out of scope. This way, we do not have to manually delete the object like it was the case with the new and delete operators.
Smart pointers are declared in the <memory> header. We will cover the following smart pointers – unique and shared.
35.1 Unique Pointer
This example creates a pointer to an object of type int and assigns a value of 123 to the object. A unique pointer can be dereferenced in the same way we as a regular pointer using the *p notation. The object gets deleted once p goes out of scope, which in this case, is at the closing brace }. No explicit use of delete operator is required.
The std::make_unique function was introduced in the C++14 standard. Make sure to compile with the -std=c++14 flag to be able to use this function.
The object gets destroyed once p goes out of scope. So, prefer a unique pointer to raw pointer and their new-delete mechanism. Once p goes out of scope, the pointed-to object of a class gets destroyed.
Neat ha? No need to explicitly delete the allocated memory, the smart pointer does it for us. Hence the smart part.
35.2 Shared Pointer
We can have multiple pointers point to a single object. We can say that all of them own our pointed-to object, that is, our object has shared ownership. And only when last of those pointers get destroyed, our pointed to object gets deleted. This is what a shared pointer is for. Multiple pointers pointing to a single object, and when all of them get out of scope, the object gets destroyed.
When all pointers get out of scope, the pointed-to object gets destroyed, and the memory for it gets deallocated.
With unique pointers, we have one pointer pointing at and owning a single object, whereas with shared pointers we have multiple pointers pointing at and owning a single object.
Unique pointers can not be copied, whereas shared pointers can.
If you wonder which one to use, let us say that 90% of the time, you will be using the unique pointer. Shared pointers can be used to represent data structures such as graphs.
Smart pointers are class templates themselves, meaning they have member functions. We will just briefly mention they can also accept custom deleters, a code that gets executed when they get out of scope.
Notice that with smart pointers, we do not need to specify the <some_type*>, we just need to specify the <some_type>.
Prefer smart pointers to raw pointers. With smart pointers, we do not have to worry if we properly matched calls to new with calls to delete as we do not need them. We let the smart pointer do all the heavy lifting.