Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/allocator/allocate at least"

From cppreference.com
< cpp‎ | memory‎ | allocator
m (Notes: -)
m (Notes: c)
Line 28: Line 28:
 
{{tt|allocate_at_least}} is mainly provided for contiguous containers, e.g. {{lc|std::vector}} and {{lc|std::basic_string}}, in order to reduce reallocation by making their capacity match the actually allocated size when possible.
 
{{tt|allocate_at_least}} is mainly provided for contiguous containers, e.g. {{lc|std::vector}} and {{lc|std::basic_string}}, in order to reduce reallocation by making their capacity match the actually allocated size when possible.
  
The "unspecified when and how" wording makes it possible to [[cpp/language/new#Allocation|combine or optimize away heap allocations]] made by the standard library containers, even though such optimizations are disallowed for direct calls to {{tt|::operator new}}. For example, this is implemented by libc++ ([https://github.com/llvm-mirror/libcxx/blob/master@%7B2017-02-09%7D/include/memory#L1766-L1772] and [https://github.com/llvm-mirror/libcxx/blob/master@%7B2017-02-09%7D/include/new#L211-L217]).
+
The "unspecified when and how" wording makes it possible to [[cpp/language/new#Allocation|combine or optimize away heap allocations]] made by the standard library containers, even though such optimizations are disallowed for direct calls to {{c|::operator new}}. For example, this is implemented by libc++ ([https://github.com/llvm-mirror/libcxx/blob/master@%7B2017-02-09%7D/include/memory#L1766-L1772] and [https://github.com/llvm-mirror/libcxx/blob/master@%7B2017-02-09%7D/include/new#L211-L217]).
  
 
After calling {{tt|allocate_at_least}} and before construction of elements, pointer arithmetic of {{c|T*}} is well-defined within the allocated array, but the behavior is undefined if elements are accessed.
 
After calling {{tt|allocate_at_least}} and before construction of elements, pointer arithmetic of {{c|T*}} is well-defined within the allocated array, but the behavior is undefined if elements are accessed.

Revision as of 10:07, 15 October 2021

 
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
std::allocator
Member functions
(until C++20)
allocator::allocate_at_least
(C++23)
(until C++20)
(until C++20)
Non-member functions
 
[[nodiscard]] constexpr std::allocation_result<T*>
    allocate_at_least( std::size_t n );
(since C++23)

Allocates count * sizeof(T) bytes of uninitialized storage, where count is an unspecified integer value not less than n, by calling ::operator new (an additional std::align_val_t argument might be provided), but it is unspecified when and how this function is called.

Then, this function creates an array of type T[count] in the storage and starts its lifetime, but does not start lifetime of any of its elements.

In order to use this function in a constant expression, the allocated storage must be deallocated within the evaluation of the same expression.

Use of this function is ill-formed if T is an incomplete type.

Contents

Parameters

n - the lower bound of number of objects to allocate storage for

Return value

std::allocation_result<T*>{p, count}, where p points to the first element of an array of count objects of type T whose elements have not been constructed yet.

Exceptions

Throws std::bad_array_new_length if std::numeric_limits<std::size_t>::max() / sizeof(T) < n, or std::bad_alloc if allocation fails.

Notes

allocate_at_least is mainly provided for contiguous containers, e.g. std::vector and std::basic_string, in order to reduce reallocation by making their capacity match the actually allocated size when possible.

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_at_least 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.

See also

[static] (C++23)
allocates storage at least as large as the requested size via an allocator
(public static member function of std::allocator_traits<Alloc>) [edit]