Allocators
An allocator is a policy class that defines an interface for managing
dynamic memory. You already know about the new and delete expressions for allocating and freeing
dynamic memory. They are simple, expressive, and useful, but the
standard library does not necessarily use them internally. Instead, the
standard library uses allocators, which let you provide alternative
mechanisms for allocating and freeing memory.
The standard library provides a standard allocator (see <memory> in Chapter 13). If you don’t want to use
the standard allocator, you can use your own, provided it satisfies the
same interface that is defined by the standard allocator.
Using Allocators
An allocator is a simple object that manages dynamic memory,
abstracting new and delete expressions. All the container class
templates take an allocator template parameter and use the allocator
to manage their internal memory. You can use allocators in your own
container classes or wherever you want to offer flexibility to the
user of your class or template.
If you do not want to bother with allocators, you don’t need to.
All the standard containers have a default argument for their
allocator template parameters: std::allocator, which uses standard new and delete expressions to manage dynamic
memory.
If you write a new container class template, make sure it takes an allocator parameter, as the standard containers do. Use the allocator to manage internal memory for your container. See Chapter 10 for more information ...