Examveda

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

A. X::operator delete(void*)

B. Freeing 400 bytes

C. Depends on the compiler

D. Both X::operator delete(void*) & Depends on the compiler

Answer: Option D


Join The Discussion

Related Questions on C plus plus miscellaneous

What is the difference between '++i' and 'i++' in C++?

A. None of the above

B. They both have the same effect

C. '++i' increments the value of 'i' before returning it, while 'i++' increments the value of 'i' after returning it

D. '++i' increments the value of 'i' after returning it, while 'i++' increments the value of 'i' before returning it