Allocators

Any package provided by the Standard C++ Library must be portable to many different machine architectures. Portability is one of C++'s strengths and the standards process helps to ensure that all compiler vendors have a common base to address this need.

One of the main problems to address in portability is the memory model of the machine. The memory model contains information about pointer types, the type of the difference between two pointers, the size of objects and also which primitives are used to allocate and deallocate raw memory. The STL encapsulates this information in a special class called an allocator. Allocators separate the STL from the dependencies of the underlying memory model of the machine architecture.

Each container is given an allocator when it is constructed. Whenever a container inserts or removes an element, it uses its allocator to allocate and deallocate the memory for the object. The container does not know anything about the memory model of the machine, it relies on the allocator for all of its memory needs.