Free Store(C++ Programming ) Questions and Answers
Explanation:-
Answer: Option C. -> Segmentation faultIn this program, The memory will go beyond the limit, So there will be exhaustion in memory.
Output:
$ g++ free4.cpp
$ a.out
free store addr = 0x80a8008
Segmentation fault
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
Explanation:-
Answer: Option C. -> Runtime errorIn this program, We are giving a location to two variables in the program, So it is arising an exception.
Output:
$ g++ free3.cpp
$ a.out
X::operator new(size_t)
X::operator new(size_t, 0)
X::operator new(size_t, 1)
X::operator new(size_t, 2)
10000
10001
10002
X::operator new(size_t, 0)
Error: buffer location occupied
Explanation:-
Answer: Option D. -> Both a & cThe memory value allocated for the program depends on compiler ony.
$ g++ free2.cpp
$ a.out
X :: operator delete(void*)
Freeing 400 bytes
Explanation:-
Answer: Option D. -> WarningIn this program, the behavior of the statement delete[] bp is undefined.
$ g++ a.cpp
a.cpp: In static member function 'static void A::operator delete [](void*, size_t)':
a.cpp:12: warning: deleting 'void*' is undefined
a.cpp: In static member function 'static void B::operator delete [](void*, size_t)':
a.cpp:20: warning: deleting 'void*' is undefined
$ a.out
~A()
~A()
~A()
A :: operator delete[]
Explanation:-
Answer: Option B. -> B::operator deleteIn this program, We are passing the value to the B, So we are printing B::operator delete.
Output:
$ g++ free.cpp
$ a.out
B::operator delete