Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Allocators and memory resources

An allocator determines how a container obtains and releases storage. std::pmr resources let you select allocation behavior at runtime without changing the container type, but require careful ordering of object lifetimes.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

using Vector = std::vector<T, Allocator>;
using AllocatorTraits = std::allocator_traits<Allocator>;
using PolymorphicAllocator = std::pmr::polymorphic_allocator<T>;
std::pmr::vector<T> values(std::pmr::polymorphic_allocator<T>{&resource});

An allocator describes a storage allocation model. In std::vector<T, Allocator>, its type is part of the container type; the default is std::allocator<T>. std::allocator_traits<Allocator> provides a uniform interface for allocation, deallocation, and element construction and destruction. Obtaining storage is not the same as invoking element constructors; the container normally manages that separation.

std::pmr::memory_resource provides a polymorphic interface that allocates blocks by byte count and alignment. std::pmr::polymorphic_allocator<T> translates requests involving T into that interface. std::pmr::vector<T> uses this allocator: two such vectors can have the same type but different resources selected at runtime. The allocator does not take ownership of the resource.

std::pmr::monotonic_buffer_resource allocates successive portions of a buffer; individual deallocation reclaims nothing. When space runs out, it uses its upstream resource. In the example, std::pmr::null_memory_resource() makes that request throw std::bad_alloc instead of obtaining additional storage. reserve(3) avoids reallocations during the next three push_back operations, but the exact capacity selected is implementation-dependent.

The buffer must outlive the resource using it, and the resource must outlive the container. Declaration order in the example ensures reverse destruction order: values, resource, buffer. Do not call release() while the container still needs its allocated storage; releasing the resource does not invoke destructors of stored elements.

This entry targets C++23. The supplied URL serves an evolving working draft, not a frozen C++23 edition. The displayed code was run unchanged in an isolated environment, and its standard output matched the stated output exactly. Verification in the production runtime is separate: neither this check nor the repository compiler flag establishes that the example works in that environment.

Examples

#include <array>
#include <cstddef>
#include <iostream>
#include <memory_resource>
#include <vector>

int main() {
    alignas(int) std::array<std::byte, 1024> buffer{};
    std::pmr::monotonic_buffer_resource resource{
        buffer.data(), buffer.size(), std::pmr::null_memory_resource()};
    std::pmr::vector<int> values{&resource};
    values.reserve(3);
    values.push_back(10);
    values.push_back(20);
    values.push_back(30);

    std::cout << values.size() << '\n';
    std::cout << values[0] + values[1] + values[2] << '\n';
}

Result

3
60

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources