C++ memory_resource::construct() Function



The C++ memory_resource::construct() function is the part of memory management system provided by the allocator class. It is specifically used to allocate memory for object at specific location in the memory that has been previously allocated by the allocate() function.

It allow us to manually create objects in pre-allocated memory block, which give us fine control over memory management.

Exception: We should only call construct() function for the memory that was allocated using the allocate() function. If you try to construct object in memory that was not allocated using the allocate() function, it will result in undefined behavior.

Syntax

Following is the syntax for std::memory_resource::construct() function.

void * construct (T * p, Args... args);

Parameters

  • p − It indicates the pointer to the pre-allocated memory block where the object will be created.
  • args − It indicates the arguments that will be passed to the constructor of the object.

Return Value

It returns a pointer to the object created in the pre-allocated memory block.

Example 1

Now, we will understand how to create an array of objects in pre-allocated memory block using construct() function.

In the this example, first we have created a class Person with two data members age and name. Then we have created an array of objects of class Person using the allocate() function. Then we have created objects in the pre-allocated memory block using the construct() function. and at last we called show function that prints the age and name of the person.

#include <iostream>
#include <string>
#include <memory>
using namespace std;

class Person {
   public:
      int age;
      string name;
      
      Person(int age, string name) {
         this->age = age;
         this->name = name;
      }

      void show() {
         cout << "Age: " << age << endl;
         cout << "Name: " << name << endl;
      }
};
int main(){
   allocator<Person> alloc;
   Person* p = alloc.allocate(5);

   alloc.construct(p, 25, "Ren");
   alloc.construct(p+1, 17, "Isagi");
   alloc.construct(p+2, 18, "Kaneki");

   p->show();
   (p+1)->show();
   (p+2)->show();
}

Output

Output of the above code is as follows −

Age: 25
Name: Ren
Age: 17
Name: Isagi
Age: 18
Name: Kaneki

Example 2

In this example, we will see another example where we will create a custom object using construct() function.

Unlike previous example, in this example we are creating a custom object MyClass with two data members id and name. Then we have created an array of objects of class MyClass using the allocate() function. Then we have created objects in the pre-allocated memory block using the construct() function.

#include <iostream>
#include <string>
#include <memory>
   using namespace std;
   
class MyClass {
public:
   MyClass(int id, const std::string& name) : id(id), name(name) {}
   
   void print() {
      cout << "ID: " << id << endl;
      cout << "Name: " << name << endl;
   }
   
private:
   int id;
   std::string name;
};
   
int main() {
   std::allocator<MyClass> alloc;
   MyClass* p = alloc.allocate(2);
   alloc.construct(p, 1, "John");
   alloc.construct(p + 1, 2, "Doe");
   
   p->print();
   (p + 1)->print();
   
   alloc.destroy(p);
   alloc.destroy(p + 1);
   
   alloc.deallocate(p, 2);
   
   return 0;
}

Output

Following is the output of the above code −

ID: 1
Name: John
ID: 2
Name: Doe
cpp_memory_resource.htm
Advertisements