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

履歴 編集

function template
<memory>

std::allocator::alloacte_at_least(C++23)

namespace std {
  [[nodiscard] constexpr allocation_result<T*>
    allocate_at_least(size_t n); // (1) C++23
}

概要

指定した要素数以上のメモリを確保する。

多くのメモリアロケータはメモリ確保時に指定されたサイズちょうどではなく、少し大きなサイズを確保する。この関数は、確保されたメモリへのポインタに加えて、実際に確保されたメモリサイズを取得できる。

適格要件

戻り値

確保された配列の先頭要素へのポインタをptrn以上である実際に確保された要素数をcountとして、

return allocation_result<T*>{ptr, count};

例外

備考

  • 配列のメモリは::operator newを呼び出すことで確保できるが、その関数がいつ・どれくらいの頻度で呼び出されるかは未規定
  • この関数は配列オブジェクトの生存期間を開始するが、配列要素の生存期間は開始しない

#include <iostream>
#include <memory>

int main() {
  std::allocator<int> alloc;

  std::allocation_result<int*> r = alloc.allocate_at_least(3);
  std::cout << "allocation count:" << r.count
            << " bytes:" << sizeof(int) * r.count
            << std::endl;

  alloc.deallocate(r.ptr, r.count);
}

出力例

allocation count:4 bytes:16

バージョン

言語

  • C++23

処理系

関連項目

参照