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 the memory leaks by making the deallocation function automatic: when the pointer to an object (or the last in a series of pointers) is destroyed, for example because it goes ouside 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
See also: reference (computer science).