In the previous recipe, we've learned how to manage dynamic memory (allocated on the heap) in a very convenient way, by using unique_ptr. We've learned as well that unique_ptr must be used, just in case of exclusive ownership of the memory, or resources managed by the memory. But what if we have a resource that is co-owned by more entities? What if the memory we have to manage has to be released when all the owners have completed their job? Well, this is exactly the use case for shared_ptr. Just as with unique_ptr, for shared_ptr we don't have to allocate memory with new, but there is a template function (part of the C++ standard library), make_shared.




















































