Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::pmr::vector: memory resources and element allocators

Choose a memory resource for a PMR vector and see when its elements receive that same resource, in contrast with a vector that uses the default allocator.

Version: ISO C++23; implementation support may vary

Syntax

#include <vector>
#include <string>
#include <memory_resource>
#include <cstddef>

// Equivalent types (T is the element type):
// std::pmr::vector<T> = std::vector<T, std::pmr::polymorphic_allocator<T>>
// std::pmr::string = std::basic_string<char, std::char_traits<char>, std::pmr::polymorphic_allocator<char>>

// In a block scope:
std::byte buffer[4096];
std::pmr::monotonic_buffer_resource arena{buffer, sizeof buffer};
std::pmr::vector<std::pmr::string> data{&arena};
data.emplace_back("PMR");

std::pmr::vector<T> from <vector> is an alias for std::vector<T, std::pmr::polymorphic_allocator<T>>, not a separate container class. std::pmr::string from <string> is std::basic_string for char with std::pmr::polymorphic_allocator<char>. Both aliases and std::byte were introduced in C++17 and are part of C++23; implementation support may vary.

A polymorphic allocator directs allocation requests to a std::pmr::memory_resource selected at run time. In data{&arena}, you explicitly select arena as the resource for the vector's element storage. A default-constructed polymorphic allocator obtains the current resource through std::pmr::get_default_resource(). That is not the same as std::allocator<T>, the default allocator type of an ordinary std::vector<T>.

When emplace_back("PMR") runs on a std::pmr::vector<std::pmr::string>, uses-allocator construction passes the vector's resource to the new string, which uses it if it needs separate character storage. If DATA means std::pmr::string, an ordinary std::vector<DATA> still uses std::allocator<DATA> for element storage. It does not pass a resource to the string constructor in this call, so the string picks up the default PMR resource. Conversely, a std::string does not start using PMR just because it is an element of a PMR vector.

std::byte from <cstddef> is a scoped enum with underlying type unsigned char. An array of it provides raw storage here for a std::pmr::monotonic_buffer_resource from <memory_resource>. When a request cannot fit in the available space, the resource asks its upstream resource for another block. If the upstream is std::pmr::null_memory_resource(), that request throws std::bad_alloc instead.

The vector does not own the resource. Declare the buffer, resource, and container in that order so the container is destroyed first. Do not call release() while objects still depend on storage allocated by the resource. A monotonic resource does not reclaim space on individual deallocations.

Examples

#include <cstddef>
#include <iostream>
#include <memory_resource>
#include <string>
#include <type_traits>
#include <vector>

int main() {
    using DATA = std::pmr::string;
    static_assert(std::is_same_v<std::vector<DATA>::allocator_type,
                                 std::allocator<DATA>>);

    std::byte buffer[4096];
    std::pmr::monotonic_buffer_resource arena{buffer, sizeof buffer};
    std::pmr::vector<DATA> pmr_data{&arena};
    std::vector<DATA> ordinary_data;

    pmr_data.emplace_back("PMR");
    ordinary_data.emplace_back("PMR");

    std::cout << std::boolalpha
              << "PMR vector uses arena: "
              << (pmr_data.get_allocator().resource() == &arena) << '\n'
              << "PMR vector element uses arena: "
              << (pmr_data.front().get_allocator().resource() == &arena) << '\n'
              << "Ordinary vector element uses default resource: "
              << (ordinary_data.front().get_allocator().resource() ==
                  std::pmr::get_default_resource()) << '\n';
}

Result

PMR vector uses arena: true
PMR vector element uses arena: true
Ordinary vector element uses default resource: true

The static assertion checks the ordinary vector's allocator type. The three `true` results confirm resource identity, not character allocation: a short string may fit inside its own object.

Related lessons

  • C++ · Lesson 18 · Advanced · Version: 3

Sources