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 |
Consider the following C++ program
How can a C function be called in a C++ program?
Explain the operator overloading feature in C++ ?
Can we call C++ OOPS? and Why
What does it mean to declare a member function as virtual in C++?
What is a class in C++?
1 Answers Amazon, TCS, UGC Corporation,
Is deconstructor overloading possible? If yes then explain and if no Then why?
Write a C++ Program to find Addition of Two Numbers.
What does it mean to declare a member function as static in C++?
what is friend function in C++?
How to run C++ program in cmd
What are Agilent PRECOMPILERS?