std::allocator::allocate - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::allocator::allocate

From cppreference.com
< cpp‎ | memory‎ | allocator

pointer allocate( size_type n, std::allocator<void>::const_pointer hint = 0 ); | (1) | (until C++17)
T* allocate( std::size_t n, const void * hint); | (1) | (since C++17) (deprecated)
T* allocate( std::size_t n ); | (2) | (since C++17)

Allocates n * sizeof(T) bytes of uninitialized storage by calling ::operator new(std::size_t), 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`.

### [edit] Parameters

n | - | the number of objects to allocate storage for
hint | - | pointer to a nearby memory location

### [edit] Return value

Pointer to the first byte of a memory block suitably aligned and sufficient to hold an array of `n` objects of type `T`.

### [edit] Exceptions

Throws std::bad_alloc if allocation fails.

### [edit] 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])

### [edit] See also

allocate [static] | allocates uninitialized storage using the allocator (public static member function of `std::allocator_traits`) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/allocator/allocate&oldid=90181"