MCQs
None.
The operand of delete must be a pointer returned by new.
In 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
In 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
The memory value allocated for the program depends on compiler ony.
$ g++ free2.cpp
$ a.out
X :: operator delete(void*)
Freeing 400 bytes
In 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[]
Free store is a pool of memory available for you to allocate and deallocate storage for objects
during the execution of your program.
new and delete operators is used to allocate and deallocate the memory for the program.
None.
In 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