Choosing between shared_ptr constructor and make_shared in C++
The article discusses the differences between using the shared_ptr
constructor and the make_shared
function in C++. The shared_ptr
constructor allows for manual creation of a new object and the adoption of a raw pointer, while make_shared
creates a single memory allocation with a control block stacked on top of the object. The two approaches have their own advantages and disadvantages. The make_shared
version has better memory locality, but a weak pointer can prevent the entire memory block from being freed. The shared_ptr
constructor, on the other hand, only keeps the control block in memory. The choice between the two depends on the specific use case and the need for memory locality. The article also mentions std::enable_shared_from_this
as a related topic to be explored in the future.