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 do you mean by exception handling in C++?

0 Answers   Alter,


Tell us the size of a float variable.

0 Answers   Accenture,


Identify the errors in the following program. #include <iostream> using namespace std; void main() { int i=5; while(i) { switch(i) { default: case 4: case 5: break; case 1: continue; case 2: case 3: break; } i-; } }

1 Answers  


dynamic scoping is

0 Answers   Siemens,


Explain about Searching and sorting algorithms with complexities

0 Answers   Accenture,






Write a program to generate the Fibonocci Series in C++.

0 Answers   Accenture,


How to delete array of objects in C++? Proof by C++ code for proper deletion

0 Answers  


What Are The Differences Between A C++ Struct And C++ Class?

2 Answers   Wipro,


What does it mean to declare a member function as static in C++?

0 Answers   Amazon,


Discuss about iteration statements in C++ .

0 Answers   Agilent,


What kind of problems does name mangling cause?

0 Answers   Amazon,


What is the difference between malloc, calloc and realloc?

0 Answers   Alter,


Categories