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

How compile and run c++ program in turbo c++?

629


Why c++ is not a pure oop language?

553


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

546


What is the full form of oops?

607


What are the benefits of interface?

577






the maximum length of a character constant can be a) 2 b) 1 c) 8

600


Should I learn c or c++ first?

563


What are the five basic elements of a c++ program?

579


Whats oop mean?

586


Is c++ the hardest language?

560


Define namespace in c++?

646


What is the use of c++ programming language in real life?

561


Is c++ map a hash table?

563


What is class in oop with example?

614


Write a program to find the Fibonacci series recursively.

601