Home » C++ Programming » Dynamic Memory » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    struct OperatorA
    {
    virtual ~OperatorA()
    {
    cout << "~OperatorA()" << endl;
    }
    void operator delete[](void* p, size_t)
    {
    cout << "Operator A :: deleteed" << endl;
    delete [] p;
    }
    };
    struct OperatorB : OperatorA
    {
    void operator delete[](void* p, size_t)
    {
    cout << "Operator B :: operator deleteed" << endl;
    delete [] p;
    }
    };
    int main()
    {
    OperatorA* ptr = new OperatorB[2];
    delete[] ptr;
    };
    1. ~OperatorA()
      ~OperatorA()
      Operator A :: deleteed
    2. ~OperatorA()
      ~OperatorA()
    3. ~OperatorA()
      Operator A :: deleteed
    4. Operator A :: deleteed
      ~OperatorA()
      ~OperatorA()
    5. None of these
Correct Option: A

In this program, the behavior of the statement delete[] ptr is undefined.



Your comments will be displayed only after manual approval.