Smart pointer

From wiki.gis.com
Jump to: navigation, search

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 introduces the risk that 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();

Example

Let SmartPointer<X> be a template smart pointer for memory management of class X instances through reference counting.

void test_smartpointers() 
{
   //first, two objects are created  and raw pointers are attached to them
   //since these pointers are not smart, they will not affect the object lifecycle 
   //since obj_1 isn't held by a smart pointer yet, if obj_2 throws a memory 
   //allocation error, obj_1 will not be destroyed
   Object* obj_1 = new Object();		
   Object* obj_2 = new Object();	
 
   //then two smart pointers are declared and assigned with the objects (obj_1 and obj_2)
   //both obj_1 and obj_2 will have counter==1
   SmartPointer<Object> p = obj_1;			
   SmartPointer<Object> q = obj_2;
 
   //now p is assigned into q, yielding obj_1.counter==2
   //obj_2 will be destroyed because its counter reaches 0
   q = p;
 
   //now q is assigned with NULL
   //obj_1.counter reaches 1
   q = NULL;
 
   //now a new object is created, and its address is assigned 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, another object is created 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();		
}

See also

  • auto_ptr
  • 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++

External links