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. These additional features are intended to reduce bugs caused by the use of pointers while retaining efficiency. Smart pointers typically keep track of the objects they point to for the purpose of memory management.
Pointer usage 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, then this use of a smart pointer is unnecessary.
Likewise, a handle is conceptually similar to a pointer, but whereas a pointer literally contains the address of the item to which it refers, a handle may be an opaque reference or obfuscated in some other way. The advantage of a handle is often that the block to which it refers can be relocated without invalidating the handle, something that is not possible with a pointer. Handles were a popular solution to memory management in operating systems of the 1980s, such as the Mac OS and Windows, but have fallen out of favour in recent times, where increases in the amount of memory available on a machine, and improved virtual memory algorithms have made the use of the simpler pointer more attractive. Unix file descriptors may also be thought of as an early example of a handle.
Like other desktop environments, the Windows API heavily uses a handle to represent objects in the system and provide a communication pathway between the operating system and its application programs. For example, a window on the desktop is represented as a handle called HWND
.
See also
External links
- Sample chapter "Smart Pointers (http://www.informit.com/articles/article.asp?p=25264)" from the book Modern C++ Design (http://www.moderncppdesign.com/) by Andrei Alexandrescu
- Code example "countptr.hpp (http://www.josuttis.com/libbook/cont/countptr.hpp.html)" from the book The C++ Standard Library - A Tutorial and Reference (http://www.josuttis.com/libbook/) by Nicolai M. Josuttis
- Article "Smart Pointers in Boost (http://cuj.com/documents/s=8470/cuj0204karlsson/)" [1] (http://boost.org/libs/smart_ptr/smart_ptr.htm)
- Article "The New C++: Smart(er) Pointers (http://cuj.com/documents/s=7980/cujcexp2008sutter/)" by Herb Sutter
- "Smart Pointers - What, Why, Which? (http://ootips.org/yonat/4dev/smart-pointers.html)" by Yonat Sharon
- "Smart Pointers Overview (http://dlugosz.com/Repertoire/refman/Classics/Smart%20Pointers%20Overview.html)" by John M. Dlugosz