C++ std::list get_allocator() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::list

Definition

In C++, std::list::get_allocator() returns a copy of the allocator that the std::list uses to get memory for its nodes. An allocator is a small helper object that creates and frees memory.

Most programs use the default std::allocator and never need this function. It matters for custom allocators and for generic code. Include <list> to use it.

This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.

Syntax

Call get_allocator with empty brackets. It returns the allocator by value.

C++
std::allocator<int> a = l.get_allocator();
int* p = a.allocate(2);   // raw memory for two ints
a.deallocate(p, 2);       // give it back
CallWhat it does
l.get_allocator()Returns a copy of the allocator object, of type allocator_type.

The returned allocator creates memory for int. The list itself rebinds it internally to build its own nodes.

Library declarations
C++
allocator_type get_allocator() const noexcept;

Parameters

ParameterDescription
Noneget_allocator() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
allocator_typeA copy of the allocator. For a plain std::list<int> this is std::allocator<int>.

C++ Examples

Example 1 — get_allocator() to Get Raw Memory

Take the allocator out of a list, allocate room for two ints with it, and give the memory back.

C++
#include <iostream>
#include <list>
#include <memory>

int main() {
    std::list<int> l;

    std::allocator<int> alloc = l.get_allocator();
    int* p = alloc.allocate(2);

    p[0] = 10;
    p[1] = 20;
    std::cout << p[0] << ' ' << p[1] << '\n';

    alloc.deallocate(p, 2);
    return 0;
}

How It Works

  1. l.get_allocator() hands out a copy of the allocator that the empty list uses. It is a std::allocator<int>.
  2. allocate(2) returns a pointer to raw memory for two int values, and the program writes 10 and 20 into it.
  3. deallocate(p, 2) gives the memory back. The list l never owned it, so the list is not changed at all.

Example 2 — get_allocator() to Compare Two Lists

Two lists can swap nodes safely only when their allocators are equal.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> a = {1, 2};
    std::list<int> b = {3};

    std::cout << std::boolalpha;
    std::cout << "equal allocators: " << (a.get_allocator() == b.get_allocator()) << '\n';

    a.splice(a.end(), b);
    std::cout << "size of a: " << a.size() << '\n';
    return 0;
}

How It Works

  1. Both lists use the default std::allocator<int>. Allocators of this kind always compare equal.
  2. Equal allocators mean memory from one list can be freed by the other, so splice() may simply relink the node.
  3. After the splice, list a holds the three elements 1 2 3, and the output shows a size of 3.

Example 3 — get_allocator() and Its Type

Ask the compiler what type the allocator has.

C++
#include <iostream>
#include <list>
#include <memory>
#include <type_traits>

int main() {
    std::list<int> l;

    using A = decltype(l.get_allocator());

    std::cout << std::boolalpha;
    std::cout << "is std::allocator<int>: "
              << std::is_same<A, std::allocator<int>>::value << '\n';
    return 0;
}

How It Works

  1. decltype(l.get_allocator()) gives the type that the function returns, without calling it.
  2. std::is_same compares that type with std::allocator<int> at compile time.
  3. A list without a custom allocator uses the default, so ::value is true.

Frequently Asked Questions

A copy of the allocator object that the list uses to create and free its nodes. By default it is std::allocator<T>.
Rarely. It is useful when you use a custom allocator, or when you write generic code that must create objects with the same allocator as a container.
An allocator is a helper object that gets and frees raw memory for a container. Containers such as std::list use it every time they create or destroy a node.
No. You get a copy. The list keeps using its own allocator.
splice() moves nodes between lists. This is only safe if the memory from one list may be freed by the other, which is the case when both allocators compare equal.

Next: insert()

Add one or more elements before an iterator with std::list.

insert() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful