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
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