[default.allocator]

# 20 Memory management library [[mem]](./#mem)

## 20.2 Memory [[memory]](memory#default.allocator)

### 20.2.10 The default allocator [default.allocator]

#### [20.2.10.1](#general) General [[default.allocator.general]](default.allocator.general)

[1](#general-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L1956)

All specializations of the default allocator meet the
allocator completeness requirements ([[allocator.requirements.completeness]](allocator.requirements.completeness "16.4.4.6.2 Allocator completeness requirements"))[.](#general-1.sentence-1)

[ð](#lib:allocator)

namespace std {template<class T> class allocator {public:using value_type = T; using size_type = size_t; using difference_type = ptrdiff_t; using propagate_on_container_move_assignment = true_type; constexpr allocator() noexcept; constexpr allocator(const allocator&) noexcept; template<class U> constexpr allocator(const allocator<U>&) noexcept; constexpr ~allocator(); constexpr allocator& operator=(const allocator&) = default; constexpr T* allocate(size_t n); constexpr allocation_result<T*> allocate_at_least(size_t n); constexpr void deallocate(T* p, size_t n); };}

[2](#general-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L1988)

allocator_traits<allocator<T>>​::​is_always_equal​::​value is true for any T[.](#general-2.sentence-1)

#### [20.2.10.2](#allocator.members) Members [[allocator.members]](allocator.members)

[1](#allocator.members-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L1994)

Except for the destructor, member functions of the default allocator shall not introduce
data races ([[intro.multithread]](intro.multithread "6.10.2 Multi-threaded executions and data races")) as a result of concurrent calls to those member
functions from different threads[.](#allocator.members-1.sentence-1)

Calls to these functions that allocate or deallocate a
particular unit of storage shall occur in a single total order, and each such
deallocation call shall happen before the next allocation (if any) in this order[.](#allocator.members-1.sentence-2)

[ð](#lib:allocate,allocator)

`constexpr T* allocate(size_t n);
`

[2](#allocator.members-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2007)

*Mandates*: T is not an incomplete type ([[basic.types.general]](basic.types.general#term.incomplete.type "6.9.1 General"))[.](#allocator.members-2.sentence-1)

[3](#allocator.members-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2011)

*Returns*: A pointer to the initial element of an array of n T[.](#allocator.members-3.sentence-1)

[4](#allocator.members-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2015)

*Throws*: bad_array_new_length ifnumeric_limits<size_t>​::​max() / sizeof(T) < n, orbad_alloc if the storage cannot be obtained[.](#allocator.members-4.sentence-1)

[5](#allocator.members-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2021)

*Remarks*: The storage for the array
is obtained by calling ​::​operator new ([[new.delete]](new.delete "17.6.3 Storage allocation and deallocation")),
but it is unspecified when or how often this
function is called[.](#allocator.members-5.sentence-1)

This function starts the lifetime of the array object,
but not that of any of the array elements[.](#allocator.members-5.sentence-2)

[ð](#lib:allocate_at_least,allocator)

`constexpr allocation_result<T*> allocate_at_least(size_t n);
`

[6](#allocator.members-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2037)

*Mandates*: T is not an incomplete type ([[basic.types.general]](basic.types.general#term.incomplete.type "6.9.1 General"))[.](#allocator.members-6.sentence-1)

[7](#allocator.members-7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2041)

*Returns*: allocation_result<T*>{ptr, count},
where ptr is a pointer to
the initial element of an array of count T andcount â¥ n[.](#allocator.members-7.sentence-1)

[8](#allocator.members-8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2048)

*Throws*: bad_array_new_length if numeric_limits<size_t>​::​max() / sizeof(T) < n,
or bad_alloc if the storage cannot be obtained[.](#allocator.members-8.sentence-1)

[9](#allocator.members-9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2054)

*Remarks*: The storage for the array is obtained by calling ​::​operator new,
but it is unspecified when or how often this function is called[.](#allocator.members-9.sentence-1)

This function starts the lifetime of the array object,
but not that of any of the array elements[.](#allocator.members-9.sentence-2)

[ð](#lib:deallocate,allocator)

`constexpr void deallocate(T* p, size_t n);
`

[10](#allocator.members-10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2068)

*Preconditions*: 

- [(10.1)](#allocator.members-10.1)

  If p is memory that was obtained by a call to allocate_at_least,
let ret be the value returned andreq be the value passed as the first argument to that call[.](#allocator.members-10.1.sentence-1)
  p is equal to ret.ptr andn is a value such that req â¤ n â¤ ret.count[.](#allocator.members-10.1.sentence-2)

- [(10.2)](#allocator.members-10.2)

  Otherwise, p is a pointer value obtained from allocate[.](#allocator.members-10.2.sentence-1)
  n equals the value passed as the first argument
to the invocation of allocate which returned p[.](#allocator.members-10.2.sentence-2)

[11](#allocator.members-11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2083)

*Effects*: Deallocates the storage referenced by p[.](#allocator.members-11.sentence-1)

[12](#allocator.members-12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2087)

*Remarks*: Uses​::​operator delete ([[new.delete]](new.delete "17.6.3 Storage allocation and deallocation")),
but it is unspecified
when this function is called[.](#allocator.members-12.sentence-1)

#### [20.2.10.3](#allocator.globals) Operators [[allocator.globals]](allocator.globals)

[ð](#lib:operator==,allocator)

`template<class T, class U>
  constexpr bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
`

[1](#allocator.globals-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2104)

*Returns*: true[.](#allocator.globals-1.sentence-1)
