What is placement new?



What is placement new?..

Answer / ritesh pal

When you want to call a constructor directly, you use the placement new. Sometimes you have some raw memory that's already been allocated, and you need to construct an object in the memory you have. Operator new's special version placement new allows you to do it.
class Widget
{
public :
Widget(int widgetsize);
...
Widget* Construct_widget_int_buffer(void *buffer,int widgetsize)
{
return new(buffer) Widget(widgetsize);
}
};
This function returns a pointer to a Widget object that's constructed within the buffer passed to the function. Such a function might be useful for applications using shared memory or memory-mapped I/O, because objects in such applications must be placed at specific addresses or in memory allocated by special routines.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ Interview Questions

What is an abstract class in C++

0 Answers   SwanSoft Technologies,


Define an Abstract class in C++?

0 Answers  


How to reverse a string in C++

0 Answers  


If class D is derived from a base class B

0 Answers  


What is the difference between realloc() and free() in C++?

0 Answers   IBS, TCS,






What are string library functions(syntax).

0 Answers   Accenture,


When must you use a constructor initializer list?

0 Answers   Amazon,


How to convert integer to string in C++

0 Answers  


What is placement new?

1 Answers   Amazon,


Tell us the size of a float variable.

0 Answers   Accenture,


What is a virtual base class?

6 Answers   Fidelity, Siemens,


Explain the difference between method overriding and method overloading in C++?

0 Answers   Accenture,


Categories