Dynamic Memory


  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. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

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


  1. What is the output of this program?
    #include <iostream>
    #include <new>
    using namespace std;
    struct N
    {
    virtual ~N() { };
    void operator delete(void* ptr1)
    {
    cout << "N :: operator delete" << endl;
    }
    };
    struct M : N
    {
    void operator delete(void* ptr1)
    {
    cout << "M :: operator delete" << endl;
    }
    };
    int main()
    {
    N* ptr2 = new M;
    delete ptr2;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    In this program, We are passing the value to the M, So we are printing M::operator delete.



  1. What type of class member is operator new?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    static


  1. Which option is best to eliminate the memory problem?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    Virtual destructor means is that the object is destructed in reverse order in which it was constructed and the smart pointer will delete the object from memory when the object goes out of scope.



  1. What is the size of the heap?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Size of the heap memory is limited by the size of the RAM and the swap memory.