In C++, std::deque::get_allocator() returns a copy of the allocator object that the deque uses to get and free memory. An allocator is a small helper object that knows how to reserve memory for elements.
Most programs use the default std::allocator<T> and never call this function. It is useful in advanced code, for example to create another container that uses the same allocator. Include <deque> to use it.
This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.
Syntax
Call get_allocator with empty brackets. It takes no arguments.
C++
auto alloc = d.get_allocator();
std::deque<int> other(alloc);
Call
What it does
d.get_allocator()
Returns a copy of the deque allocator, of type allocator_type.
std::deque<int> other(alloc)
Creates a new empty deque that uses the same allocator.
For a default deque the type is std::allocator<T>. You can also write it as std::deque<T>::allocator_type.
Library declarations
C++
allocator_type get_allocator() const noexcept;
Parameters
Parameter
Description
None
get_allocator() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Allocator
A copy of the allocator in use. Changing the copy does not change the deque.
C++ Examples
Example 1 — Check the Allocator Type
A plain deque uses std::allocator by default.
C++
#include <deque>
#include <iostream>
#include <memory>
#include <type_traits>
int main() {
std::deque<int> d;
std::allocator<int> a = d.get_allocator();
std::cout << std::boolalpha;
std::cout << "allocator_type is std::allocator<int>: "
<< std::is_same<std::deque<int>::allocator_type, std::allocator<int>>::value << '\n';
std::cout << "same as a new allocator: " << (a == std::allocator<int>()) << '\n';
return 0;
}
Output
allocator_type is std::allocator<int>: true
same as a new allocator: true
How It Works
d.get_allocator() returns the allocator of the deque. Here it fits into a std::allocator<int> variable.
std::is_same compares two types at compile time. The answer true shows that the default allocator type is std::allocator<int>.
Default allocators of the same type are always equal, so a == std::allocator<int>() is true.
Example 2 — Get Memory with the Allocator
Borrow the deque allocator to reserve space for three ints.
C++
#include <deque>
#include <iostream>
#include <memory>
int main() {
std::deque<int> d;
auto alloc = d.get_allocator();
int* p = alloc.allocate(3);
for (int i = 0; i < 3; ++i) {
p[i] = (i + 1) * 10;
}
for (int i = 0; i < 3; ++i) {
std::cout << p[i] << ' ';
}
std::cout << '\n';
alloc.deallocate(p, 3);
return 0;
}
Output
10 20 30
How It Works
alloc.allocate(3) reserves raw memory for three ints and returns a pointer to it. The deque itself is not touched.
The loops store and print 10 20 30. This is fine for simple types such as int.
deallocate(p, 3) gives the memory back. Every allocate() needs a matching deallocate() with the same size.
Example 3 — Create Another Deque with the Same Allocator
Pass the allocator to the constructor of a second deque.
std::deque<int> b(a.get_allocator()) creates a new empty deque that uses a copy of the allocator of a.
The constructor only copies the allocator, not the elements, so b.size() is 0.
Both deques now use equal allocators, so memory from one could be released by the other. The comparison prints true.
Frequently Asked Questions
It returns a copy of the allocator object that the deque uses to get and free memory.
Rarely. Most programs use the default std::allocator and never call it. It matters for custom allocators and advanced container code.
An allocator is a small object that knows how to reserve and release memory for elements. Standard containers ask their allocator for memory instead of calling new directly.
No. You get a copy. The deque keeps its own allocator.
Yes. Every standard container has it, and it works in the same way.