|
|
{ }
and it's getting the resource pointed by ptr.
|
|
|
|
|
|
code such as f(std::shared_ptr<int>(new int(42)), g()) can cause a memory leak if g throws an exception because g() may be called after new int(42) and before the constructor of shared_ptr<int>. This doesn't occur in f(std::make_shared<int>(42), g()), since two function calls are never interleaved. http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared |
std::make_shared<T> typically allocates memory for the T object and for the std::shared_ptr's control block with a single memory allocation (this is a non-binding requirement in the Standard), where std::shared_ptr<T>(new T(args...)) performs at least two memory allocations. http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared |
new T(args...)
The shared_ptr objects that own a resource share a control block. The control block holds: . the number of shared_ptr objects that own the resource, . the number of weak_ptr objects that point to the resource, . the deleter for that resource if it has one, . the custom allocator for the control block if it has one. https://msdn.microsoft.com/en-us/library/bb982026.aspx |