- memory_resource[meta header]
- function[meta id-type]
- std::pmr[meta namespace]
- memory_resource[meta class]
- cpp17[meta cpp]
void deallocate(void* p, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t));- std::max_align_t[link /reference/cstddef/max_align_t.md]
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));
}- deallocate[color ff0000]
- get_default_resource[link /reference/memory_resource/get_default_resource.md]
- allocate[link /reference/memory_resource/memory_resource/allocate.md]
- std::destroy_at[link /reference/memory/destroy_at.md]
256
000002373BB96970
000002373BB96970
- C++17
- Clang: ??
- GCC: 9.1 [mark verified]
- Visual C++: 2017 update 6 [mark verified]