delete operator overloading in cpp

Joined
Mar 25, 2009
Messages
1
Reaction score
0
hi all,
i hv a problem in cpp. i want to overload the new and delete operator
and i done it, but after execution of program, i donot know why but delete
operator is executing three times more than the required.
here i m giving program and its output also.
i want to know its reason.
#include<iostream.h>
#include<alloc.h>
#include<conio.h>
#include<stdlib.h>

class loc
{
int longitude, latitude;
public:
loc() {longitude=1;}
~loc(){cout<<"\nDestructor\n";}

loc(int lg, int lt)
{
longitude = lg; latitude = lt;
}

void show()
{
cout <<"\n\n"<< longitude<<" "<<latitude;
}
};

// Global new
void *operator new(size_t size)
{
cout<<"\n In global new:";
void *p;
p = malloc(size);
return p;
}

// Global delete
void operator delete(void *p)
{
cout<<"\nIn global delete:";
free(p);
}


void main()
{
loc *p1, *p2;
float *f;
clrscr();
// p1 = new loc (10,20);
// p2 = new loc (20,30);
// p3 = new loc (-10, -20);

// f=new float; //uses overloaded new, too

// p1->show();
// p2->show();

// *f = 10.10F;
// cout << *f;

// delete p1;
getch();
// delete p2;
// exit(0);
getch();
delete f;
// getch();
}


output::

in global delete
in global delete
in global delete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top