Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Containers

C++ allocators and std::allocator

Reference for C++ allocators: how containers obtain memory, std::allocator, allocator_traits, writing a minimal custom allocator and stateful versus stateless allocators.

An allocator is an object that a container asks for raw memory and gives it back to. Every standard container has an allocator as its last template parameter, which defaults to std::allocator<T>: a thin wrapper over operator new and operator delete. Replacing it lets you count allocations, take memory from an arena, align storage specially or use a shared pool without changing the container's code.

An allocator only provides and releases storage. Constructing and destroying the elements is done by the container, through std::allocator_traits, which fills in defaults for any member your allocator omits. A minimal modern allocator needs just a value_type, allocate, deallocate and the equality operators.

C++
std::vector<int, MyAllocator<int>> v;

Try it

C++ A counting allocator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ allocator_traits and std::allocator directly

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class T> struct allocator {
    using value_type = T;
    [[nodiscard]] constexpr T* allocate(std::size_t n);
    constexpr void deallocate(T* p, std::size_t n);
};
Member Description
value_type The element type. Required.
allocate(n) Returns storage for n objects of T, suitably aligned, without constructing them. Throws std::bad_alloc on failure.
deallocate(p, n) Releases storage from an earlier allocate(n) with the same n. Must not throw.
converting constructor A template<class U> Alloc(const Alloc<U>&); containers use it to rebind your allocator to node types.
operator== Two allocators are equal if memory from one can be freed by the other.

Optional members, defaulted by std::allocator_traits: construct, destroy, max_size, select_on_container_copy_construction, the propagate_on_container_* flags and is_always_equal. In C++20 std::allocator::construct/destroy were removed in favor of std::construct_at.

Errors / undefined behavior: deallocating with a different pointer, a different size or an unequal allocator is undefined behavior. allocate should throw std::bad_alloc (or std::bad_array_new_length), never return null.

Usage notes

  • Stateless allocators (empty classes) cost nothing in the container. Stateful ones (pools, arenas) are stored inside it, and two instances with different state must compare unequal.
  • Propagation: whether copy-assignment, move-assignment and swap carry the allocator along is controlled by propagate_on_container_copy_assignment, ..._move_assignment and ..._swap. Getting these wrong gives containers that cannot be moved cheaply.
  • The type is part of the container type: vector<int, A> and vector<int> are unrelated types and cannot be assigned to each other. Polymorphic memory resources solve this by making the allocator type fixed and the resource a run-time object.
  • Rebinding: node-based containers allocate internal node types, not T, so an allocator must be constructible from an allocator of any other type.
  • std::allocator_traits<A>::allocate + construct is the pattern for writing your own container; most programs never call allocators directly.
  • Alignment: allocate must honor over-aligned types; use ::operator new(size, std::align_val_t{...}) for those.
  • When to bother: profile first. Typical gains come from arena allocators (many short-lived objects freed together) and pools for same-size nodes.