Thursday, March 06, 2008

Overloading the NEW and DELETE operators in C++

Operator Overloading - One of the important pillars of the Object Oriented Concept is a very useful tool in the hands of the right programmer.

The fact that you can even Overload the NEW operator and the DELETE operator as well adds convenience to the custom memory management.

1. Overloading the NEW Operator:

void *operator new(size_t size, int setvalue)
{
void *pointer;

p = malloc(size);
if(p==NULL)
outofMemory( );
memset(pointer,setvalue,size);
return(p);
}

void outofMemory( )
{
cout<<"OOPS ran out of free memory..";
exit(1);
{.....perform other memory freeing operations here and try to re-allocate the memory....}
}

1 comment:

QuackWare said...

Cool Stuff, can you check out my C++ Code Samples too?