Write simple program to overload new and delete operator in C++

Answer includes a simple program to overload new and delete operator in C++ class with important points about overloaded new and delete. Below is the C++ code example program for overloading new and delete operator.

C++ syntax for overloading new and delete is simple. The new operator will accept object size in its parameter and will return a void pointer. The delete operator will accept the pointer do be freed and does not return anything, means return void.

Now if we create an object of the class overloaded new and delete operator function will be called for memory allocation rather than directly requesting memory to operating system.

#include<iostream>
using namespace std;

class CustomMemory{

public: 

	void* operator new(size_t objectSize);//Overloaded new 
	void operator delete(void* ptr); //Overloaded delete
};

void* CustomMemory::operator new(size_t objectSize)
{
	cout<<"Custom memory allocation"<<endl;
	//May Write costume memory allocation algorithm here
	return malloc(objectSize);

}
void CustomMemory::operator delete(void* ptr)
{
	cout<<"Custom memory de- allocation"<<endl;
	free(ptr);
}


int main(){

	// call overloaded new from the class
	CustomMemory *obj = new CustomMemory();
	// call overloaded delete 
	delete obj;
}
Output:
Custom memory allocation
Custom memory de- allocation

NOTES:

If above question is asked then please don’t forget to mention below points.

Important Points/properties: new and delete operator overloading
  • Overloaded new operator can accept additional arguments except size to be allocated.
  • In one class delete operator can be overloaded only once.
  • In one class new operator can be overloaded multiple times.
  • When we create object of the class with “new” operator the class overloaded new function will get called with the object size as a parameter. e.g. CustomMemory object = new CustomMemory.

C++ program example – Overloaded new operator multiple times:

#include<iostream>
using namespace std;


class CustomMemory{
	int i;// size of int is 4 byte 
public:
	CustomMemory(){
		cout<<"Constructor"<<"\n";
	}
	~CustomMemory(){
		cout<<"Destructor"<<"\n";
	}
	//Overloaded new
	void* operator new(size_t objectSize) {
		cout<<"Custom memory allocation"<<"\n";
		//Write allocation algorithm here
		return malloc(objectSize);
	}
	//Overloaded 2 arguments new operator
	void* operator new(size_t objectSize, int x) {
		cout<<"Custom 2 argument memory allocation"<<"\n"; 
		CustomMemory *ptr =(CustomMemory*)malloc(objectSize);
		ptr->i = x;
		return ptr;
	}

	//Overloaded delete 
	void operator delete(void* ptr) {
		cout<<"Custom memory de- allocation"<<"\n";
		free(ptr);
	}
	void Display(){
		cout<<"Value of i ="<<i<<"\n"; 
	} 
};
int main(){

	// call overloaded new from the class
	CustomMemory *obj = new CustomMemory();
	// call overloaded delete
	delete obj;

	//overloaded 2 argument new
	CustomMemory * ptr = new(5)CustomMemory();
	ptr->Display();
	delete ptr;
}

Output:
Custom memory allocation
Constructor
Destructor
Custom memory de- allocation
Custom 2 argument memory allocation
Constructor
Value of i =5
Destructor
Custom memory de- allocation

Related Posts