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

Identify the error in the following program. include<iostream> using namespace std; void main() { int num[]={1,2,3,4,5,6}; num[1]==[1]num ? cout<<"Success" : cout<<"Error"; }

1 Answers  


What is meant by exit controlled loop?

0 Answers   Adobe,


What are issues if we mix new and free in C++?

0 Answers  


Name the operators that cannot be overloaded.

1 Answers   Wipro,


Explain about Searching and sorting algorithms with complexities

0 Answers   Accenture,






How to reverse a string in C++

0 Answers  


What does malloc return in C and C++?

0 Answers   Alter,


Explain encapsulation in C++.

0 Answers   Verifone,


What is the difference between public, private, and protected inheritance?

0 Answers   Amazon,


When would you use a pointer? A reference?

0 Answers   Amazon,


How to convert integer to string in C++

0 Answers  


It is possible to build a C++ compiler on top of a C compiler. How would you do this?

0 Answers   Amazon,


Categories