Wednesday, November 2, 2011


Allocators in C++

The C++ standard library uses in several places special objects to handle the allocation and deallocation of memory. Such objects are called allocators. An allocator represents a special memory
model. It is used as abstraction to translate the need to use memory into a raw call for memory. The use of different allocator objects at the same time allows you to use different memory models in a program.

Allocators originally were introduced as part of the STL to handle the nasty problem of different pointer types on PCs (such as near, far, and huge pointers). They now serve as a base for
technical solutions that use certain memory models, such as shared memory, garbage collection, and object-oriented databases, without changing the interfaces. However, this use is relatively
new and not yet widely adopted (this will probably change).

The C++ standard library defines a default allocator as follows:

namespace std {
   template <class T>
   class allocator;

The default allocator is used as the default value everywhere an allocator can be used as an argument. It does the usual calls for memory allocation and deallocation; that is, it calls the new
and delete operators. However, when or how often these operators are called is unspecified. Thus, an implementation of the default allocator might, for example, cache the allocated memory internally.

The default allocator is used in most programs. However, sometimes other libraries provide allocators to fit certain needs. In such cases you simply must pass them as arguments. Only
occasionally does it make sense to program allocators. In practice, typically the default allocator is used.
See Also:
C++ Language Concepts and Sample Codes


  1. yellow text on white background does not work.

    1. Corrected. We will take care of other pages too.

      Support Group -