Tuesday, December 2, 2008

Smart Pointer

A smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. Smart pointers typically keep track of the objects that point to them for the purpose of memory management. In C++ language, smart pointers may be implemented as a template class that mimics, by means of operator overloading, the behavior of traditional pointers, (e.g. dereferencing, assignment) while providing additional memory management algorithms.

Why?
1. Less bugs. 2. Exception Safety. 3. Garbage collection. 4. Efficiency. 5. STL containers.

Efficiency: to make efficient use of available memory and to shorten allocation and deallocation time. A common strategy for using memory more efficiently is copy on write (COW). The same object is shared by many COW pointers as long as it is only read and not modified. The standard string class is commonly implemented using COW semantics.

Reference:
Smart pointer - Wiki

Additional Reading:
Smart pointer templates in C++ by David Harvey

No comments: