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 use of pointers while retaining efficiency. Smart pointers typically keep track of the objects they point to for the purpose of memory management.
The use 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, then this use of a smart pointer is unnecessary.
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 if 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();
Similarly smart pointers can be used to deal with the problem in C++ that pointers to arrays and pointers to single objects are the same type even though the data must be deleted differently.
[edit] Handles
A handle is a particular kind of smart pointer. Handles are used when an application references blocks of memory or objects managed by another system, such as a database or an operating system. While a pointer literally contains the address of the item to which it refers, a handle is an abstract reference controlled by a separate system; its opacity allows the referent to be relocated in memory by the system without invalidating the handle — impossible with pointers. The extra layer of indirection also increases the control the managing system has over operations performed on the referent (see information hiding, encapsulation).
Handles were a popular solution to memory management in operating systems of the 1980s, such as Mac OS and Windows. Unix file descriptors are essentially handles. Like other desktop environments, the Windows API heavily uses handles to represent objects in the system and to provide a communication pathway between the operating system and user space. For example, a window on the desktop is represented by a handle of type HWND
.
Traditional, doubly-indirect handles have fallen out of favour in recent times, as increases in available memory and improved virtual memory algorithms have made the use of the simpler pointer more attractive. However, many operating systems still apply the term to pointers to opaque, "private" data structures, or indexes into internal arrays passed from one process to its clients.
[edit] See also
- Reference
- The Standard Template Library includes a reference-counting smart pointer for C++
- The Boost library includes another smart pointer implementation for C++
[edit] External links
- Sample chapter "Smart Pointers" from the book Modern C++ Design by Andrei Alexandrescu
- 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
- Process Explorer freeware Task Manager -like tool which can show handles in Windows
- The YASPER library Yet Another Smart Pointer implementation in C++