Choosing between shared_ptr constructor and make_shared in C++

2023/08/16
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

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.