Shared Pointers
Explore the concept of shared pointers in C++ and how they manage shared ownership of resources through reference counting. Learn to use std::shared_ptr methods and std::make_shared for efficient memory management. Understand how shared pointers automatically release resources when no longer referenced, enhancing safe and reliable coding.
We'll cover the following...
std::shared_ptr shares the ownership of the resource. They have two handles. One for the resource and one for the reference counter. By copying a std::shared_ptr, the reference count is increased by one. It is decreased by one if the std::shared_ptr goes out of scope. If the reference counter becomes the value 0 and therefore there is no std::shared_ptr referencing the resource, the C++ runtime automatically releases the resource. The release of the resource takes place at exactly the ...