Choose allocation strategy at runtime without changing the container's type — the practical, low-boilerplate alternative to template allocators. Header:
<memory_resource>· Since: C++17
A std::pmr::memory_resource is an abstract base with virtual allocate /
deallocate. std::pmr::polymorphic_allocator<T> holds a pointer to one and
forwards to it. So all std::pmr::vector<int> share one type regardless of
which resource backs them — the strategy is a runtime object, not a template
parameter.
pmr::vector<int> ──holds──▶ polymorphic_allocator ──▶ memory_resource*
├─ monotonic_buffer_resource
├─ (un)synchronized_pool_resource
└─ new_delete_resource (default)
| Resource | Behavior |
|---|---|
std::pmr::new_delete_resource() |
default; wraps new/delete |
std::pmr::monotonic_buffer_resource |
allocate-only; frees everything at once on destruction — extremely fast |
std::pmr::synchronized_pool_resource |
pooled, thread-safe |
std::pmr::unsynchronized_pool_resource |
pooled, single-thread (faster) |
std::pmr::null_memory_resource() |
always throws — caps a buffer so overflow fails loudly |
#include <memory_resource>
#include <vector>
std::byte buffer[4096];
std::pmr::monotonic_buffer_resource arena{buffer, sizeof buffer};
void demo() {
std::pmr::vector<int> v{&arena}; // allocates out of `buffer` — no heap, no per-element free
for (int i = 0; i < 100; ++i) v.push_back(i);
std::pmr::string s{"hello", &arena}; // same arena; all reclaimed when `arena` dies
}When arena is destroyed, every allocation it made is released in one shot — no
per-object deallocation, ideal for request-scoped or frame-scoped work.
pmrcontainers default tonew_delete_resourceif you don't pass one — so forgetting the&arenaargument silently uses the heap.- Lifetime: the resource must outlive every container using it. A
monotonic_buffer_resourceover a stack buffer is a dangling trap if the buffer goes out of scope first. monotonic_buffer_resourcenever frees individual allocations — memory grows until the resource dies. That's the point (speed), but watch total usage in long-lived arenas.pmr::vector<T>≠std::vector<T>. They're different types (different allocator), so they don't implicitly interconvert.