 st8b7c:f320:99b9:690f:4595:cd17:293a:c069pmr8b7c:f320:99b9:690f:4595:cd17:293a:c069null_memory_resource    Defined in header <memory_resource>         st8b7c:f320:99b9:690f:4595:cd17:293a:c069pmr8b7c:f320:99b9:690f:4595:cd17:293a:c069memory_resource* null_memory_resource() noexcept;
    (since C++17)    Returns a pointer to a memory_resource that doesn't perform any allocation.
  Return value  Returns a pointer p to a static storage duration object of a type derived from st8b7c:f320:99b9:690f:4595:cd17:293a:c069pmr8b7c:f320:99b9:690f:4595:cd17:293a:c069memory_resource, with the following properties:

 its allocate() function always throws st8b7c:f320:99b9:690f:4595:cd17:293a:c069bad_alloc; 
 its deallocate() function has no effect; 
 for any memory_resource r, p->is_equal(r) returns &r == p. 
 The same value is returned every time this function is called.
 Example 
The program demos the main usage of null_memory_resouce: ensure that a memory pool which requires memory allocated on the stack will NOT allocate memory on the heap if it needs more memory.
#include <array>
#include <cstddef>
#include <iostream>
#include <memory_resource>
#include <string>
#include <unordered_map>
 
int main()
{
    // allocate memory on the stack
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069array<st8b7c:f320:99b9:690f:4595:cd17:293a:c069byte, 20000> buf;
 
    // without fallback memory allocation on heap
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069pmr8b7c:f320:99b9:690f:4595:cd17:293a:c069monotonic_buffer_resource pool{ buf.data(), buf.size(),
                                              st8b7c:f320:99b9:690f:4595:cd17:293a:c069pmr8b7c:f320:99b9:690f:4595:cd17:293a:c069null_memory_resource() };
 
    // allocate too much memory
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069pmr8b7c:f320:99b9:690f:4595:cd17:293a:c069unordered_map<long, st8b7c:f320:99b9:690f:4595:cd17:293a:c069pmr8b7c:f320:99b9:690f:4595:cd17:293a:c069string> coll{ &pool };
    try
    {
        for (st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t i = 0; i < buf.size(); ++i)
        {
            coll.emplace(i, "just a string with number " + st8b7c:f320:99b9:690f:4595:cd17:293a:c069to_string(i));
 
            if (i && i % 50 == 0)
                st8b7c:f320:99b9:690f:4595:cd17:293a:c069clog << "size: " << i << "...\n";
        }
    }
    catch(const st8b7c:f320:99b9:690f:4595:cd17:293a:c069bad_alloc& e)
    {
        st8b7c:f320:99b9:690f:4595:cd17:293a:c069cerr << e.what() << '\n';
    }
 
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << "size: " << coll.size() << '\n';
} Possible output:
size: 50...
size: 100...
size: 150...
st8b7c:f320:99b9:690f:4595:cd17:293a:c069bad_alloc
size: 183  

    
{'path': 'public/docs/cpp/memory/null_memory_resource.html', 'source': 'DevDocs.IO'}