?
What will be the output of the following C++ code?
What will be the output of the following C++ code?
#include <cstdlib>
#include <iostream>
using namespace std;
class X
{
public:
void* operator new(size_t sz) throw (const char*)
{
void* p = malloc(sz);
if (p == 0)
throw "malloc() failed";
return p;
}
void operator delete(void* p)
{
cout << "X :: operator delete(void*)" << endl;
free(p);
}
};
class Y
{
int filler[100];
public:
void operator delete(void* p, size_t sz) throw (const char*)
{
cout << "Freeing " << sz << " bytes" << endl;
free(p);
};
};
int main()
{
X* ptr = new X;
delete ptr;
Y* yptr = new Y;
delete yptr;
}
#include <cstdlib>
#include <iostream>
using namespace std;
class X
{
public:
void* operator new(size_t sz) throw (const char*)
{
void* p = malloc(sz);
if (p == 0)
throw "malloc() failed";
return p;
}
void operator delete(void* p)
{
cout << "X :: operator delete(void*)" << endl;
free(p);
}
};
class Y
{
int filler[100];
public:
void operator delete(void* p, size_t sz) throw (const char*)
{
cout << "Freeing " << sz << " bytes" << endl;
free(p);
};
};
int main()
{
X* ptr = new X;
delete ptr;
Y* yptr = new Y;
delete yptr;
}
Join the Discussion
Login to post a comment or share your explanation.
Login