Smart pointer
From Wikipedia, the free encyclopedia
In computer science, a smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. These additional features are intended to reduce bugs caused by the misuse of pointers while retaining efficiency. Smart pointers typically keep track of the objects that point to them for the purpose of memory management.
The misuse of pointers is a major source of bugs: the constant allocation, deallocation and referencing that must be performed by a program written using pointers makes it very likely that some memory leaks will occur. Smart pointers try to prevent memory leaks by making the resource deallocation automatic: when the pointer to an object (or the last in a series of pointers) is destroyed, for example because it goes out of scope, the pointed object is destroyed too.
Several types of smart pointers exist. Some work with reference counting, others assigning ownership of the object to a single pointer. If the language supports automatic garbage collection (for instance, Java), then this use of a smart pointer is unnecessary.
In C++ language, smart pointers may be implemented as a template class that mimics, by means of operator overloading, the behaviour of traditional (raw) pointers, (e.g.: dereferencing, assignment) while providing additional memory management algorithms.
Smart pointers can facilitate intentional programming by expressing the use of a pointer in the type itself. For example, if a C++ function returns a pointer, there is no way to know whether the caller should delete the memory pointed to when the caller is finished with the information.
some_type* ambiguous_function(); // What should be done with the result?
Traditionally, this has been solved with comments, but this can be error-prone. By returning a C++ auto_ptr
,
auto_ptr<some_type> obvious_function1();
the function makes explicit that the caller will take ownership of the result, and further more, that if the caller does nothing, no memory will be leaked. Similarly, if the intention is to return a pointer to an object managed elsewhere, the function could return by reference:
some_type& obvious_function2();
[edit] Example
Let SmartPointer<X> be a template smart pointer for memory management of class X instances through reference counting.
void test_smartpointers() { //first, we create two objects and we keep raw pointers to them //since these pointers are not smart, they will not affect the object lifecycle Object* obj_1 = new Object(); Object* obj_2 = new Object(); //then we declare two smart pointers and we assign them with the objects //both obj_1 and obj_2 will have counter==1 SmartPointer<Object> p = obj_1; SmartPointer<Object> q = obj_2; //now we assign p into q, yielding obj_1.counter==2 //obj_2 will be destroyed because its counter reaches 0 q = p; //we assign q with NULL //obj_1.counter reaches 1 q = NULL; //now we create a new object, and we assign its address to the smart pointer //it will be automatically destroyed before leaving the scope //obj_1 will be destroyed because its counter reaches 0 p = new Object(); //finally, we create another object which will be only referenced by a raw pointer. //obj_3 will be lost and there will be a memory leak Object* obj_3 = new Object(); }
[edit] See also
- Opaque pointer
- Reference
- The Standard Template Library includes a smart pointer for C++
- The Boost library includes a reference-counting smart pointer implementation for C++
[edit] External links
- Sample chapter "Smart Pointers" from the book Modern C++ Design: Generic Programming and Design Patterns Applied by Andrei Alexandrescu, Addison-Wesley, 2001.
- Code example "countptr.hpp" from the book The C++ Standard Library - A Tutorial and Reference by Nicolai M. Josuttis
- Article "Smart Pointers in Boost" [1]
- Article "The New C++: Smart(er) Pointers" by Herb Sutter
- "Smart Pointers - What, Why, Which?" by Yonat Sharon
- "Smart Pointers Overview" by John M. Dlugosz
- The YASPER library Yet Another Smart Pointer implementation in C++