Campus interview zone
Would you like to react to this message? Create an account in a few clicks or log in to continue.

What is placement new?

Go down

What is placement new? Empty What is placement new?

Post  Admin Sun Jan 31, 2010 5:49 pm

Answer:
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.

Admin
Admin

Posts : 71
Join date : 2010-01-21

http://campus-interview.co.cc

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum