auto_ptr

shared_ptr is more heavyweight than scoped_ptr. It needs to allocate and free a reference count object as well as the managed object, and to handle thread-safe reference counting - on one platform I worked on, this was a significant overhead.

My advice (in general) is to use the simplest object that meets your needs. If you need reference-counted sharing, use shared_ptr; if you just need automatic deletion once you've finished with a single reference, use scoped_ptr.

I always advise people to start with boost::scoped_ptr. If you need transfer of ownership semantics (maintaining single ownership) then "upgrade" to std::auto_ptr. If you need shared ownership only then do you use boost::shared_ptr. Also, the Boost.ptr_container library is a nice alternative to containers of boost::shared_ptr when the elements don't really need to be shared

Reference:
http://stackoverflow.com/questions/1770636/shared-ptr-vs-scoped-ptr
shared_ptr may leak?!

c++
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License