Loading....
Coupon Accepted Successfully!

 

Overloading NEW and DELETE Operators

In C++, at any time when memory allocation and deallocation is concerned, the new and delete operators are used. These operators are invoked from the compiler’s library functions. They are a part of C++ language and are very effective. Similar to other operators, the new and delete operators are overloaded. The following programs illustrate this:
 

14.9 Write a program to overload new and delete operator.

#include<iostream.h>

#include<stdlib.h>

#include<new.h>

#include<conio.h>

void main()

{

clrscr();

void warning();

void *operator new (size_t, int);

void operator delete (void*);

char *t=new (‘#’) char [10];

cout<<endl<<“First allocation:p=”<<(unsigned)long(t)<<endl;

for (int k=0;k<10;k++)

cout<<t[k];

delete t;

t=new (‘*’) char [64000u];

delete t;

}

void warning()
{

cout<<“\n insufficient memory”;
exit(1);

}
void *operator new (size_t os, int setv)
{

void *t;
t=malloc(os);
if (t==NULL) warning();
memset(t,setv,os);
return (t);

}
void operator delete(void *ss) {free(ss);}

OUTPUT
First allocation : p=3376
##########
Insufficient memory
 
Explanation: In the above program, the new and delete operators are overloaded. The size_t is used to determine the size of the object. The new operator calls the warning() function when the malloc() function returns NULL.

Consider the statement t = new (‘*’) char [64000u]. When memory allocation is requested by this statement, the new operator fails to allocate the memory and calls the function warning(). When called, the delete operator releases the memory using the free() function. Internally, in this program, malloc() and free() functions are used to allocate and deallocate the memory.
 

14.10 Write a program to overload new and delete operator.

#include<iostream.h>

#include<stdlib.h>

#include<new.h>

#include<conio.h>

void main()

{

clrscr();

void warning();

void *operator new (size_t);

void operator delete (void*);

char *t;

t=new char[5];

cout<<endl<<“First allocation:p=”<<(unsigned)long(t)<<endl;
for (int k=0;k<10;k++)
{

*(t+k)=‘-’;
cout<<*(t+k);

}
delete t;
t=new char [64000U];
delete t;

}

void warning()

{

cout<<“\nInsufficient memory”;
exit(1);

}
void *operator new (size_t os)
{

void *t;
t=malloc(os);
if (t==NULL)
warning();
return (t);

}
void operator delete(void *ss) {free(ss);}

OUTPUT
First allocation: p=3374
----------
Insufficient memory

Explanation:
The above program is similar to the previous one. In overloading of the new operator, only one argument is used. In the previous examples, the overloaded new and delete operators use the malloc() and free() function to allocate and deallocate memory. The same can be done with new and delete operators. The following programs illustrate the overloading of new and delete operators:
 

14.11 Write a program to overload new and delete operator. Read five numbers and find largest out of them.

#include<iostream.h>

#include<conio.h>

const int NUM=5;

class number

{

private :
int *series;
public :

void *operator new (size_t)
{

number *num;
num=::new number;
num->series=new int[NUM];
return num;

}
void operator delete ( void* n)
{

number *num;
num=(number *) n;
delete (int *) num->series;
::delete n;

}

void input();
int large (void);

};
void number ::input()
{

for (int i=0;i<NUM;i++)
{

cout<<“Number[”<<i+1<<“]=”;
cin>>series[i];

}

}
int number ::large()
{

int l=0;
for (int i=0;i<NUM;i++)

{
if (l<series[i])
l=series[i];
}

return l;
}
void main()
{

clrscr();
number *l_num=new number;
cout<<“Enter numbers”<<endl;
l_num->input();

cout<<“Largest Number:”<<l_num->large();
delete l_num;

}

OUTPUT
Enter numbers
Number[ 1] = 4
Number[ 2] = 7
Number[ 3] = 8
Number[ 4] = 2
Number[ 5] = 3
Largest Number : 8

Explanation:
In the above program, the new and delete operators are overloaded. The new operator creates objects as well as allocates data members. The statement number *l_num = new number executes the overloaded new operator. The statement num =:: new numberand ::delete n creates and destroys objects respectively. The number::new and number::delete are recursively executed. The statement num =::new number creates an object of the number class. If the scope access operator is omitted, the overloaded operator is executed recursively and results in a stack overflow.

If we prefix the scope access operator to the new operator, it will invoke the standard new operator built-in language instead of a defined one. The class number has a data member variable *series. The statement num->series = new int [NUM ] creates an array of NUM elements and allocates memory. It invokes the ::new operator.

Similarly, the overloaded delete operator releases the memory allocated by the new operator. It executes the overloaded operator function void operator delete (void* n).




Test Your Skills Now!
Take a Quiz now
Reviewer Name