C++ New::operator new



With the new keyword in C++, it is possible to dynamically allocate memory at runtime in the RAM's heap segment. At the time of declaration, a parameter specifying the allocated memory size is passed. Memory can be allocated for both preset and unique data types using the new operator.

The function operator new, which allocates raw memory, is conceptually identical to malloc ().

  • It is the mechanism of overriding the default heap allocation logic.
  • The operator new can also be overloaded either globally or for a particular class.

Syntax

Following is the syntax for C++ New::operator new −

void* operator new (std::size_t size) throw (std::bad_alloc);       (throwing allocation)
void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw();                (nothrow allocation)
void* operator new (std::size_t size, void* ptr) throw(); 

Parameters

  • size − It contain size in bytes of the requested memory block.
  • nothrow_value − It contains the constant nothrow.
  • ptr − It is a pointer to an already-allocated memory block of the proper size.

Example 1

Let's look into the following example, where we are going to use the operator new and retriveing the output.

#include <cstdio>
#include <cstdlib>
#include <new>
void* operator new(std::size_t sz) {
   std::printf(" new(size_t), size = %zu\n", sz);
   if (sz == 1)
      ++sz;
   if (void *ptr = std::malloc(sz))
      return ptr;
   throw std::bad_alloc{};
}
int main() {
   int* p1 = new int;
   delete p1;
}

Output

Let us compile and run the above program, this will produce the following result −

 new(size_t), size = 4

Example 2

Let's look into the another scenario, where we are going to use the operator new.

#include <iostream>
#include <new>
struct MyClass {
   int data[100];
   MyClass() {
      std::cout << "It constructed [" << this << "]\n";
   }
};
int main () {
   std::cout << "1: ";
   MyClass * p1 = new MyClass;
   std::cout << "2: ";
   MyClass * p2 = new (std::nothrow) MyClass;
   std::cout << "3: ";
   new (p2) MyClass;
   MyClass * p3 = (MyClass*) ::operator new (sizeof(MyClass));
   delete p1;
   delete p2;
   delete p3;
   return 0;
}

Output

On running the above code, it will display the output as shown below −

1: It constructed [0x5596f5df72c0]
2: It constructed [0x5596f5df7460]
3: It constructed [0x5596f5df7460]

Example 3

Considering the following where we are going to check the functioning of operator new.

#include<iostream>
#include<stdlib.h>
using namespace std;
class car {
   string name;
   int num;
   public:
   car(string a, int n) {
      cout << "TutorialsPoint" << endl;
      this->name = a;
      this->num = n;
   }
   void display() {
      cout << "Name: " << name << endl;
      cout << "Num: " << num << endl;
   }
   void *operator new(size_t size) {
      cout << "Welcome" << endl;
      void *p = malloc(size);
      return p;
   }
};
int main() {
   car *p = new car("RX100", 2011);
   p->display();
   delete p;
}

Output

when the code gets executed, it will generate the output as shown below −

Welcome
TutorialsPoint
Name: RX100
Num: 2011
Advertisements