adplus-dvertising
frame-decoration

Question

What is the output of this program?
    #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: (d).Both X::operator delete(void*) & Depends on the compiler

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the output of this program?