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 |
Explain the difference between method overriding and method overloading in C++?
What are pass by value and pass by reference?what is the disadvantage of pass by value?
When would you choose to return an error code rather than throw an exception?
Name the operators that cannot be overloaded.
How to delete array of objects in C++? Proof by C++ code for proper deletion
Write a C++ Program to Display Number (Entered by the User).
Tell How To Check Whether A Linked List Is Circular ?
What is function overloading and operator overloading in C++?
What is an abstract class?
what is friend function in C++?
What is C++11?
What is bool in C++