C++ provides global “new” and global “delete” operators to allocate & deallocate memory. However, it is possible to overload these operators in context of a specific-class. Thereafter, all construction and destruction of objects of that class shall happen using these overloaded operators.
Overload new & delete :
We can control the creation & destruction of both types of objects, i.e. a single object as well as any arrays of objects. The respective classes need to overload these operators. To make the things consistent, the class needs to overload both the array versions as well as the regular version of both new/delete operators in the class.
In C++, the syntax to overload array versions look very similar to regular versions for new and delete, except they have square brackets.
Single Object
For creation of a single object, following two operators may be defined by the class MainFunda.
void* MainFunda::operator new(size_t sz)
void MainFunda::operator delete(void* m)
Therefore, following specific commands shall call above overloads.
MainFunda *obj = new MainFunda( );
delete obj;
Array Objects
For creation of array of objects, a class, like, MainFunda shall overload following operators. These are similar, but have extra squrare brackets ([ ]).
void* MainFunda::operator new[](size_t sz)
void MainFunda::operator delete[](void* m)
Therefore, following specific commands invoke these
MainFunda *obj = new MainFunda[4];
delete [] obj;
Only array version shall be overloaded in this example:
In following example, firstly, the compiler will call global version of new and delete when program creates or destroys a single object.
Secondly, the compiler will call the overload version (special new[] and delete[]) when program creates or destroy an array of objects. During creation, the compiler shall first call the overloaded operator new and then constructor. Similarly, during destruction, the compiler call in reverse order. It first calls destructor and then calls overloaded delete.
#include <iostream> //main header
using namespace std;//namespace
class MainFunda
{
public:
MainFunda()
{
cout << "MainFunda()" << endl;
}
void* operator new[](size_t sz)
{
cout << "Overloaded operator\
new[]: size="
<< sz
<< " Bytes"
<< endl;
void* m = malloc(sz);
if(!m)
{
cout << "no memory allocated"
<< endl;
}
return m;
}
void operator delete[](void* m)
{
cout << "Overloaded operator \
delete[]"
<< endl;;
free(m);
}
~MainFunda()
{
cout << "~Mainfunda()"
<< endl;
}
};
int main()
{
cout << "Creating & Destroying \
single object" << endl;
MainFunda * a= new MainFunda;
//Global new will be called
delete a;
//Global delete will be called
cout << "\nCreating & Destroying \
array object"
<< endl;
MainFunda * aarray= new MainFunda[5];
//Overloaded new will be called
delete [] aarray;
//Overloaded delete called
return 0;
}
The output is:
Creating & Destroying single object
MainFunda()
~MainFunda()
Creating & Destroying array object
Overloaded operator new[]: size=13 Bytes
MainFunda()
MainFunda()
MainFunda()
MainFunda()
MainFunda()
~MainFunda()
~MainFunda()
~MainFunda()
~MainFunda()
~MainFunda()
Overloaded operator delete[]