Tags

links:
http://en.wikipedia.org/wiki/Smart_pointer
http://www.codeproject.com/Articles/15351/Implementing-a-simple-smart-pointer-in-c
http://www.codeproject.com/Articles/327248/Understanding-Implementing-Smart-Pointer-in-C
http://stackoverflow.com/questions/8199812/difference-between-boostscoped-ptrt-and-stdunique-ptrt
http://stackoverflow.com/questions/1770636/shared-ptr-vs-scoped-ptr
http://stackoverflow.com/questions/4984381/shared-ptr-and-weak-ptr-differences
http://stackoverflow.com/questions/692438/is-boost-shared-ptr-xxx-thread-safe

summary:
– auto_ptr deprecated mainly due to its implicit transfer of ownership causing segfault at runtime
– in general smart ptrs using a stack object to wrap a heap object or memory allocation, and override the ctors dtors and operator “=” to limit and control the behavior of the internal pointers
– when out of scope, the destructor of the stack object would be triggered, e.g. free your heap memory automatically
– shared_ptr uses operator “=” to increase “reference count” of shared ownership
– shared_ptr is thread safe(?) and overhead for reference counting
– shared_ptr is thread safe in terms of read and increment count, not safe when the actually shared_ptr is modified.
– unique_ptr uses move() to transfer ownership
– unique_ptr owns an object exclusively. Non-copyable, supports transfer-of-ownership, introduced to replace auto_ptr
– scope_ptr not copyable nor movable, and is gone when goes out of scope.
– weak_ptr is a scale down version of shared_ptr that doesn’t affect refcount, but uses the value so it should check valid before using it

Advertisements