pointer allocate( size_type n, std::allocator<void>::const_pointer hint = 0 );
|
(1) | (C++17 前) |
T* allocate( std::size_t n, const void * hint);
|
(1) | (C++17 起) (弃用) |
(2) | ||
T* allocate( std::size_t n );
|
(C++17 起) (C++20 前) |
|
[[nodiscard]] T* allocate( std::size_t n );
|
(C++20 起) | |
以调用 ::operator new(std::size_t) 分配 n * sizeof(T) 字节的未初始化存储,但何时及如何调用此函数是未指定的。指针 hint
可用于提供引用的局部性:若实现支持,则 allocator 会试图分配尽可能接近 hint
的新内存块。
目录 |
n | - | 要分配存储的对象数 |
hint | - | 指向临近内存位置的指针 |
指向适当对齐并足以保有 T
类型的 n
个对象数组的内存块首字节的指针。
若分配失败则抛出 std::bad_alloc 。
遣词“未指定何时及如何”令标准库容器可以组合或优化掉堆分配,即使这种优化对直接调用 ::operator new
被禁止。例如,这为 libc++ 所实现( [1] 与 [2] )
[静态]
|
用分配器分配未初始化的存储 ( std::allocator_traits 的公开静态成员函数)
|