What is placement new?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

2. Give the different notations for the class.\

1790


What causes a runtime error c++?

811


What is ios flag in c++?

871


Explain selection sorting. Also write an example.

803


What is constructor and destructor in c++?

847


What is the use of main function in c++?

729


What is virtual methods?

878


What are the uses of pointers?

779


How many types of comments are there in c++?

755


What is the use of string in c++?

738


How long to learn object oriented programming?

801


what is COPY CONSTRUCTOR and what is it used for?

808


Are vectors faster than arrays?

757


How do pointers work?

919


What is a singleton c++?

763