• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <memory>

    std::allocator::allocate

    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);
    }
    

    出力

    関連項目

    参照