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

What is partial specialization or template specialization?

0 Answers   Amazon,


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

0 Answers   Accenture,


What does it mean to declare a member function as virtual in C++?

0 Answers   Amazon,


What is a COPY CONSTRUCTOR and when is it called?

0 Answers   IBS,


C++ Public access specifier instead of Private – What is bad ?

0 Answers  






what is the difference between a pointer and a reference?

0 Answers   Amazon, Blue Star, C DAC,


What is meant by exit controlled loop?

0 Answers   Adobe,


What is a memory leak in C++?

0 Answers   Agilent,


What Is Polymorphism in C++ ?

1 Answers   IBS, Impetus, ITC Indian Tobacco Company, Motorola,


Discuss the role of C++ shorthands.

0 Answers   Adobe,


Write a syntax and purpose of switch statement.

0 Answers   Agilent,


what is a pragma in C++?

0 Answers   Aspire,


Categories