void deallocate(void* p, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t));
概要
allocateによって確保されたメモリを解放する。
事前条件
呼び出すdo_deallocateの要件として
pの指すサイズbytesのメモリ領域は、*thisもしくは等しいmemory_resourceオブジェクト(this->is_equal(other) == trueとなるようなother)のallocate(bytes, alignment)によって事前に確保された領域であること。
かつ、そのメモリ領域は未解放であること。
引数
p-- 解放したい領域へのポインタbytes--pに確保されている領域のサイズalignment--pの確保時アライメント要求
効果
return this->do_deallocate(p, bytes, alignment); と等価。
例外
投げない
例
#include <iostream>
#include <memory_resource>
int main(){
std::pmr::memory_resource* mr = std::pmr::get_default_resource();
//int1つ分の領域をintのアライメント要求(多くの環境で共に4バイト)でメモリ確保
void* p = mr->allocate(sizeof(int), alignof(int));
//placement new して構築
int* p_int = new(p) int{ 256 };
std::cout << *p_int << std::endl;
//一応アドレスを出力
std::cout << p << std::endl;
std::cout << p_int << std::endl;
//デストラクタを呼び出してオブジェクトを破棄
std::destroy_at(p_int);
//メモリの解放
mr->deallocate(p, sizeof(int), alignof(int));
}
出力例(VS2019 Preview2)
256
000002373BB96970
000002373BB96970
バージョン
言語
- C++17
処理系
- Clang: ??
- GCC: 9.1 ✅
- Visual C++: 2017 update 6 ✅