CPP

CPP

Free Store has 8 Questions
Which is used to allocate and deallocate storage for objects during the execution?

Stack
Heap
Freestore
None of the mentioned
Which operators are used in the free store?

new
delete
Both a & b
None of the mentioned
What type of class member is operator new?

static
dynamic
const
smart
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     struct A 
  5.     {
  6.         virtual ~A() {  };
  7.         void operator delete(void* p) 
  8.         {
  9.             cout << "A :: operator delete" << endl;
  10.         }
  11.     };
  12.     struct B : A 
  13.     {
  14.         void operator delete(void* p) 
  15.         {
  16.             cout << "B :: operator delete" << endl;
  17.         }
  18.     };
  19.     int main() 
  20.     {
  21.         A* ap = new B;
  22.         delete ap;
  23.     }

A::operator delete
B::operator delete
Both a & b
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include
  3.     #include 
  4.     using namespace std;
  5.     class X;
  6.     struct Node 
  7.     {
  8.         X* data;
  9.         bool filled;
  10.         Node() : filled(false) { }
  11.     };
  12.     class X 
  13.     {
  14.         static Node buffer[];
  15.         public:
  16.         int number;
  17.         enum { size = 3};
  18.         void* operator new(size_t sz) throw (const char*)
  19.         {
  20.             void* p = malloc(sz);
  21.             if (sz == 0)
  22.                 throw "Error: malloc() failed";
  23.             cout << "X :: operator new(size_t)" << endl;
  24.             return p;
  25.         }
  26.         void *operator new(size_t sz, int location) throw (const char*) 
  27.         {
  28.             cout << "X :: operator new(size_t, " << location << ")" << endl;
  29.             void* p = 0;
  30.             if (location < 0 || location >= size || buffer[location].filled == true)
  31.             {
  32.                 throw "Error: buffer location occupied";
  33.             }
  34.             else 
  35.             {
  36.                 p = malloc(sizeof(X));
  37.                 if (p == 0) 
  38.                     throw "Error: Creating X object failed";
  39.                 buffer[location].filled = true;
  40.                 buffer[location].data = (X*) p;
  41.             }
  42.             return p;
  43.         }
  44.         static void printbuffer() 
  45.         {
  46.             for (int i = 0; i < size; i++) 
  47.             {
  48.                 cout << buffer[i].data->number << endl;
  49.             }
  50.         } 
  51.     };
  52.     Node X::buffer[size];
  53.     int main()
  54.     {
  55.         try 
  56.         {
  57.             X* ptr1 = new X;
  58.             X* ptr2 = new(0) X;
  59.             X* ptr3 = new(1) X;
  60.             X* ptr4 = new(2) X;
  61.             ptr2->number = 10000;
  62.             ptr3->number = 10001;
  63.             ptr4->number = 10002;
  64.             X :: printbuffer();
  65.             X* ptr5 = new(0) X;
  66.         }
  67.         catch (const char* message) 
  68.         {
  69.             cout << message << endl;
  70.         }
  71.     }

X::operator new(size_t)
Error
Runtime error
None of the mentioned
How can object be allocated outside the object lifetime?

int
float
void*
None of the mentioned
What must be an operand of operator delete?

Pointer
Array
Stack
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     const int bsize = 512;
  6.     int *pa;
  7.     bool allocate = true;
  8.     void get_memory() 
  9.     {
  10.         cerr << "free store exhausted" << endl;
  11.         delete [] pa;
  12.         allocate = false;
  13.     }
  14.     void eat_memory(int size) 
  15.     {
  16.         int *p = new int[size];
  17.         if (allocate)
  18.             eat_memory(size);
  19.         else
  20.             cerr << "free store addr = " << p << endl;
  21.     }
  22.     int main()
  23.     {
  24.         set_new_handler(get_memory);
  25.         pa = new int[bsize];
  26.         cerr << "free store addr = " << pa << endl;
  27.         eat_memory(bsize);
  28.         return 0;
  29.     }

free store addr
Error
Segmentation fault
None of the mentioned