- Smart pointer
In computer science, a smart pointer is an
abstract data type that simulates apointer while providing additional features, such as automatic garbage collection orbounds 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 ofmemory 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 leak s 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. Traditionally, this has been solved with comments, but this can be error-prone. By returning a C++
, 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();auto_ptr Example
Let SmartPointer
be a template smart pointer for memory management of class X instances through reference counting. See also
*
auto_ptr
*Opaque pointer
* Reference
* TheStandard Template Library includes a smart pointer for C++
* TheBoost library includes a reference-counting smart pointer implementation for C++External links
*Sample chapter " [http://www.informit.com/articles/article.aspx?p=25264 Smart Pointers] " from the book " [http://www.moderncppdesign.com/ Modern C++ Design: Generic Programming and Design Patterns Applied] " by
Andrei Alexandrescu , Addison-Wesley, 2001.
*Code example " [http://www.josuttis.com/libbook/cont/countptr.hpp.html countptr.hpp] " from the book " [http://www.josuttis.com/libbook/ The C++ Standard Library - A Tutorial and Reference] " byNicolai M. Josuttis
*Article " [http://cuj.com/documents/s=8470/cuj0204karlsson/ Smart Pointers in Boost] " [http://boost.org/libs/smart_ptr/smart_ptr.htm]
*Article " [http://cuj.com/documents/s=7980/cujcexp2008sutter/ The New C++: Smart(er) Pointers] " byHerb Sutter
*" [http://ootips.org/yonat/4dev/smart-pointers.html Smart Pointers - What, Why, Which?] " byYonat Sharon
*" [http://dlugosz.com/Repertoire/refman/Classics/Smart%20Pointers%20Overview.html Smart Pointers Overview] " byJohn M. Dlugosz
* The [http://yasper.sourceforge.net/ YASPER library] Yet Another Smart Pointer implementation in C++
* [http://barrkel.blogspot.com/2008/09/smart-pointers-in-delphi.html Smart Pointers in Delphi]
Wikimedia Foundation. 2010.