Namespaces
Variants

std::allocator_traits<Alloc>:: allocate

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (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)
(C++11) (until C++23)
定义于头文件 <memory>
static pointer allocate ( Alloc & a, size_type n ) ;
(1) (C++11 起)
(C++20 起为 constexpr)
static pointer allocate ( Alloc & a, size_type n, const_void_pointer hint ) ;
(2) (C++11 起)
(C++20 起为 constexpr)

使用分配器 a 分配 n * sizeof ( Alloc :: value_type ) 字节的未初始化存储空间。在存储空间中创建了类型为 Alloc :: value_type [ n ] 的数组,但不会构造其中的任何元素。

1) 调用 a. allocate ( n )
2) 额外传递内存局部性提示 hint 。若可能则调用 a. allocate ( n, hint ) 。若不可行(例如 a 不具有双参数成员函数 allocate ),则调用 a. allocate ( n )

目录

参数

a - 使用的分配器
n - 需要分配存储空间的对象数量
hint - 指向邻近内存位置的指针

返回值

调用 a. allocate ( n ) 所返回的指针。

注释

Alloc::allocate P0593R6 之前并不要求创建数组对象,该提案使得根据核心语言规范的严格解读,对 std::vector 及其他某些容器使用非默认分配器的行为变得未定义。

在调用 allocate 之后、元素构造之前, Alloc :: value_type * 的指针算术在已分配的数组内是明确定义的,但若访问元素则行为未定义。

示例

参见

分配未初始化的存储空间
( std::allocator<T> 的公开成员函数)