The special std::pmr::null_memory_resource() will throw an exception when anyone tries to allocate memory using it. You can safeguard from performing any allocations using pmr by setting it as the default resource as shown next:
std::pmr::set_default_resource(null_memory_resource());
You can also use it to limit allocation from the upstream when it shouldn't happen. Check the following code:
auto buffer = std::array<std::byte, 640 * 1024>{}; // 640K ought to be enough for anybody auto resource = std::pmr::monotonic_buffer_resource{ buffer.data(), buffer.size(), std::pmr::null_memory_resource()};
If anybody tries to allocate more than the buffer size we set, std::bad_alloc would be thrown.
Let's move on to our last item in this chapter.