Memory Resource class

The memory_resource class, the <memory_resource> header defines several derived classes that implement different allocation strategies. These classes include:

  • The new_delete_resource class uses the new and delete operators to allocate and deallocate memory.
  • The null_memory_resource class does not allocate any memory and always returns nullptr.
  • The monotonic_buffer_resource class allocates memory from a pre-allocated buffer.
  • The class known as pool_resource is responsible for memory allocation from a pool of memory.

Syntax:

#include <memory_resource>

Examples of Memory  Resource

Example 1: 

C++




// C++ Program to demonstrate the use of the
// <memory_resource> Header.
#include <iostream>
#include <memory_resource>
#include <vector>
  
int main()
{
    std::pmr::monotonic_buffer_resource buffer(1024);
    std::pmr::vector<int> v(&buffer);
    v.push_back(42);
  
    for (const auto& element : v) {
        std::cout << element << " ";
    }
    std::cout << std::endl;
  
    return 0;
}


Output:

42

Example 2: 

C++




// C++ Program to demonstrate the use of the
// <memory_resource> Header.
#include <iostream>
#include <memory_resource>
#include <vector>
  
int main()
{
    std::pmr::unsynchronized_pool_resource pool;
    std::pmr::vector<int> v(&pool);
  
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
  
    std::cout << "Vector elements: ";
    for (const auto& element : v) {
        std::cout << element << " ";
    }
    std::cout << std::endl;
  
    return 0;
}


Output:

Vector elements: 0 1 2 3 4 5 6 7 8 9 

When we utilize an unsynchronized_pool_resource to manage the vector, we can skip the heap allocations and make use of a memory pool for assigning memory to the components of the vector which may come in handy when there is a need to frequently assign or withdraw memory. Employing a memory pool can be more effective than the conventional method of allocating and withdrawing memory from the heap.

C++17 – Header

C++17 introduced the <memory_resource> header, which provides a mechanism for customizing the allocation and deallocation of memory in C++ programs. This header defines the memory_resource class and several derived classes that implement different memory allocation strategies. The C++ Standard Library has incorporated the header which can be accessed by including the header. You need to explicitly add the header in order to use it. In C++, memory allocation is usually achieved via the new and delete operators provided by the language. These operators are accountable for allocating and releasing memory to objects but are not always functional in every scenario.

The interface for allocating and deallocating memory is defined by the abstract base class memory_resource class. The design of fresh memory allocation methods can be achieved by crafting personalized algorithms for allocating and deallocating while exploiting the memory_resource category. One useful aspect found in this class is the is_equal function, allowing for the comparison of two memory_resource entities to verify any similarities. This feature proves particularly beneficial when confirming whether memory allocators follow the same allocation techniques.

Similar Reads

Memory Resource class

The memory_resource class, the header defines several derived classes that implement different allocation strategies. These classes include:...

Advantages of using memory_resource header

...

Contact Us