std::allocator<T>::allocate

Allocates n * sizeof(T) bytes of uninitialized storage by calling ::operator new(std::size_t) or ::operator new(std::size_t, std::align_val_t)(since C++17), but it is unspecified when and how this function is called. The pointer hint may be used to provide locality of reference: the allocator, if supported by the implementation, will attempt to allocate the new memory block as close as possible to hint.

# Declarations

pointer allocate( size_type n, const void* hint = 0 );

(until C++17)

T* allocate( std::size_t n, const void* hint );

(since C++17) (deprecated) (removed in C++20)

T* allocate( std::size_t n );

(since C++17) (until C++20)

constexpr T* allocate( std::size_t n );

(since C++20)

# Parameters

# Return value

Pointer to the first element of an array of n objects of type T whose elements have not been constructed yet.

# Notes

The “unspecified when and how” wording makes it possible to combine or optimize away heap allocations made by the standard library containers, even though such optimizations are disallowed for direct calls to ::operator new. For example, this is implemented by libc++ ([1] and [2]).

After calling allocate() and before construction of elements, pointer arithmetic of T* is well-defined within the allocated array, but the behavior is undefined if elements are accessed.

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 578C++98hint was required to be either 0 or apointer previously returned from allocate()and not yet passed to deallocate()not required
LWG 3190C++11allocate() might allocate storage of wrong sizethrows std::bad_array_new_length instead

# See also