- memory[meta header]
- std[meta namespace]
- allocator[meta class]
- function[meta id-type]
pointer allocate(size_type n); // (1) C++03
[[nodiscard]] constexpr pointer allocate(size_type n); // (1) C++20
constexpr pointer allocate(size_type n); // (1) C++26
pointer allocate(size_type n,
allocator<void>::const_pointer hint); // (2) C++17 から非推奨、C++20 から削除
メモリを確保する。
適切にアライメント配置されたn * sizeof(T)
サイズのストレージの配列の、最初の要素へのポインタを返す。
ストレージは、::operator new(std::size_t)
の呼び出しによって取得される。
この関数の呼び出し頻度やヒントの扱いは未規定。
ストレージからのメモリ確保に失敗した場合、bad_alloc
例外を送出する。
コンテナのメンバ関数でこの関数を使用する場合には、隣接要素のアドレスをヒントとして渡すのが適している。
ヒントパラメータは、アロケータ実装者に使われなかったため、非推奨となった。
#include <memory>
int main()
{
std::allocator<int> alloc;
// 10要素のint領域を確保する
std::size_t n = 10;
int* p = alloc.allocate(n);
// 確保したメモリを解放する
alloc.deallocate(p, n);
}
- allocate[color ff0000]
- alloc.deallocate[link deallocate.md]
- P0174R2 Deprecating Vestigial Library Parts in C++17
- P0600R1
[[nodiscard]]
in the Library- C++20で
[[nodiscard]]
が付加された
- C++20で
- P0619R4 Reviewing deprecated facilities of C++17 for C++20
- P0784R7 More constexpr containers
- C++20で
constexpr
が付加された
- C++20で
- P2422R1 Remove
nodiscard
annotations from the standard library specification- C++26で
[[nodiscard]]
指定が削除された
- C++26で