Home » C++ Programming » Dynamic Memory » Question
  1. What is the output of this program?
    #include <iostream>
    #include <new>
    #include <cstdlib>
    using namespace std;
    const int bsize = 128;
    int *ptr1;
    bool allocate = true;
    void get_memory()
    {
    cerr << "free store exhausted" << endl;
    delete [] ptr1;
    allocate = false;
    }
    void eat_memory(int size)
    {
    int *ptr2 = new int[size];
    if (allocate)
    eat_memory(size);
    else
    cerr << "free store addr = " << ptr2 << endl;
    }
    int main()
    {
    set_new_handler(get_memory);
    ptr1 = new int[bsize];
    cerr << "free store addr = " << ptr1 << endl;
    eat_memory(bsize);
    return 0;
    }
    1. Compilation Error
    2. Segmentation fault
    3. Free store addr
    4. Runtime Error
    5. None of these
Correct Option: B

In this program, The memory will go beyond the limit, So there will be exhaustion in memory.



Your comments will be displayed only after manual approval.