Question
#include <new>
#include<cstdlib>
#include <iostream>
using namespace std;
class X;
struct Node
{
X* data;
bool filled;
Node() : filled(false) { }
};
class X
{
static Node buffer[];
public:
int number;
enum { size = 3};
void* operator new(size_t sz) throw (const char*)
{
void* p = malloc(sz);
if (sz == 0)
throw "Error: malloc() failed";
cout << "X :: operator new(size_t)" << endl;
return p;
}
void *operator new(size_t sz, int location) throw (const char*)
{
cout << "X :: operator new(size_t, " << location << ")" << endl;
void* p = 0;
if (location < 0 || location >= size || buffer[location].filled == true)
{
throw "Error: buffer location occupied";
}
else
{
p = malloc(sizeof(X));
if (p == 0)
throw "Error: Creating X object failed";
buffer[location].filled = true;
buffer[location].data = (X*) p;
}
return p;
}
static void printbuffer()
{
for (int i = 0; i < size; i++)
{
cout << buffer[i].data->number << endl;
}
}
};
Node X::buffer[size];
int main()
{
try
{
X* ptr1 = new X;
X* ptr2 = new(0) X;
X* ptr3 = new(1) X;
X* ptr4 = new(2) X;
ptr2->number = 10000;
ptr3->number = 10001;
ptr4->number = 10002;
X :: printbuffer();
X* ptr5 = new(0) X;
}
catch (const char* message)
{
cout << message << endl;
}
}
a.
X::operator new(size_t)
b.
Error
c.
Runtime error
d.
None of the mentioned
Posted under Object Oriented Programming Using C++
Engage with the Community - Add Your Comment
Confused About the Answer? Ask for Details Here.
Know the Explanation? Add it Here.
Q. What is the output of this program?
Similar Questions
Discover Related MCQs
Q. What must be an operand of operator delete?
View solution
Q. How can object be allocated outside the object lifetime?
View solution
Q. Pick out the wrong header file about strings.
View solution
Q. Which is best for coding the standard libary for c++?
View solution
Q. What is meant by vector in the container library contains?
View solution
Q. Pick out the wrong header file.
View solution
Q. What is meant by standard c++ library?
View solution
Q. Pick out parameter for rehash method in unordered_set in c++?
View solution
Q. What is the use of <exception> header
View solution
Q. How many sets of requirements are need in designing a container?
View solution
Q. Which interface in the container is required for storage management?
View solution
Q. Which is present in the basic interface of the allocator interface?
View solution
Q. Which container provides random access iterators?
View solution
Q. Which is used for manually writing lookup table?
View solution
Q. How the member functions in the container can be accessed?
View solution
Q. Which of the following type does the container should define?
View solution
Q. Which are the parameters for the content of the buffer?
View solution
Q. What do vectors represent?
View solution
Q. In which type of storage location are the vector members stored?
View solution
Q. How many vector container properties are there in c++?
View solution
Suggested Topics
Are you eager to expand your knowledge beyond Object Oriented Programming Using C++? We've curated a selection of related categories that you might find intriguing.
Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!
Microprocessor
Understand the heart of your computer with our Microprocessor MCQs. Topics include...
Cyber Security
Understand the fundamentals of safeguarding digital assets with our Cyber Security...