Free Store

1

How can object be allocated outside the object lifetime?

A. int
B. float
C. void*
D. None of the mentioned

2

What must be an operand of operator delete?

A. Pointer
B. Array
C. Stack
D. None of the mentioned

3

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.
    }
A. free store addr
B. Error
C. Segmentation fault
D. None of the mentioned

4

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.
    }
A. X::operator new(size_t)
B. Error
C. Runtime error
D. None of the mentioned

5

What is the output of this program?

1.
    #include 
2.
    #include 
3.
    using namespace std;
4.
    class X 
5.
    {
6.
        public:
7.
        void* operator new(size_t sz) throw (const char*)
8.
        {
9.
            void* p = malloc(sz);
10.
            if (p == 0) 
11.
                throw "malloc() failed";
12.
            return p;
13.
        }
14.
        void operator delete(void* p) 
15.
        {
16.
            cout << "X :: operator delete(void*)" << endl;
17.
            free(p);
18.
        } 
19.
    };
20.
    class Y 
21.
    {
22.
        int filler[100];
23.
        public:
24.
        void operator delete(void* p, size_t sz) throw (const char*)
25.
        {
26.
            cout << "Freeing " << sz << " bytes" << endl;
27.
            free(p);
28.
        };
29.
    };
30.
    int main() 
31.
    {
32.
        X* ptr = new X;
33.
        delete ptr;
34.
        Y* yptr = new Y;
35.
        delete yptr;
36.
    }
A. X::operator delete(void*)
B. Freeing 400 bytes
C. Depends on the compiler
D. Both a & c