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

CDPATH shell variable is in(c-shell)

0 Answers   Siemens,


Explain function prototypes in C++.

0 Answers   Aricent,


What are the fundamental features of an object-oriented language?

0 Answers   Accenture,


There is a base class sub, with a member function fnsub(). There are two classes super1 and super2 which are sub classes of the base class sub.if and pointer object is created of the class sub which points to any of the two classes super1 and super2, if fnsub() is called which one will be inoked?

0 Answers   Alter,


What is bool in C++

0 Answers  






Name the operators that cannot be overloaded.

1 Answers   Wipro,


Discuss about iteration statements in C++ .

0 Answers   Agilent,


Difference between function overloading and function overriding.

0 Answers   Alter,


Describe the different styles of function prototypes in C++.

0 Answers   Adobe,


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

0 Answers   Accenture,


Consider the following C++ program

0 Answers  


Write a program to input an integer from the keyboard and display on the screen “WELL DONE” that many times.

1 Answers  


Categories