Home » C++ Programming » Dynamic Memory » Question
  1. What is the output of this program?
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    class OperatorX
    {
    public:
    void* operator new(size_t sz) throw (const char*)
    {
    void* ptr1 = malloc(sz);
    if (ptr1 == 0)
    throw "malloc() failed";
    return ptr1;
    }
    void operator delete(void* ptr1)
    {
    cout << "Operator X :: delete(void*)" << endl;
    free(ptr1);
    }
    };
    class OperatorY
    {
    int filler[150];
    public:
    void operator delete(void* ptr1, size_t sz) throw (const char*)
    {
    cout << "Freeing " << sz << " bytes" << endl;
    free(ptr1);
    };
    };
    int main()
    {
    OperatorX* ptr1 = new OperatorX;
    delete ptr1;
    OperatorY* ptr2 = new OperatorY;
    delete ptr2;
    }
    1. Operator X :: delete(void*)
    2. Freeing 600 bytes
      Operator X :: delete(void*)
    3. Freeing 600 bytes
    4. Operator X :: delete(void*)
      Freeing 600 bytes
    5. None of these
Correct Option: D

The memory value allocated for the program depends on compiler only.



Your comments will be displayed only after manual approval.