Describe the “placement new" and why would i use it?, C/C++ Programming

Describe the “placement new" and why would I use it?

Posted Date: 3/15/2013 5:36:49 AM | Location : United States

A: There are several uses of placement new. The simplest use is to place an object at a specific location in memory. It is done by supplying the place as a pointer parameter to the new part of a new expression:

#include // Must #include this to use "placement new"

#include "Fred.h" // Declaration of class Fred

void someCode()


char memory[sizeof(Fred)]; // Line #1 void* place = memory; // Line #2

Fred* f = new(place) Fred(); // Line #3 (see "DANGER" below)

// The pointers f & place will be equal



Line #1 creates an array of sizeof(Fred) bytes of memory, that is adequate to hold a Fred object. Line #2 creates a pointer place which points to the first byte of this memory (experienced C programmers will note  down that this step was needless; it''s there just to make the code more obvious). Line #3 in essence just calls the constructor Fred::Fred(). The this pointer in the Fred constructor will be equal to place. Therefore the returned pointer f will be equal to place.

ADVICE: Don''t use this "placement new" syntax if not you have to. Use it only while you really care that an object is placed at a specific location in memory. For instance, while your hardware has a memory-mapped I/O timer device, and you wished to place a Clock object at that memory location.

DANGER: You are taking solitary job that the pointer you pass to the "placement new" operator points to a region of memory which is sufficient and is appropriately aligned for the object type which you''re creating. Neither the compiler nor run-time system makes any effort to check whether you did this right. If your Fred class requires to be aligned on a 4 byte boundary however you supplied a location which isn''t properly aligned, you can have serious disaster on your hands You have been warned. You are also exclusively responsible for destructing the placed object. It is done by explicitly calling the destructor:

void someCode()


char memory[sizeof(Fred)];

void* p = memory; Fred* f = new(p) Fred();


f->~Fred(); // Explicitly call the destructor for the placed object


It is about the only time you ever explicitly call a destructor.

Note: there is a much apparent but more sophisticated way of handling the destruction / deletion situation.

Posted by | Posted Date: 3/15/2013 5:37:27 AM

Related Discussions:- Describe the “placement new" and why would i use it?, Assignment Help, Ask Question on Describe the “placement new" and why would i use it?, Get Answer, Expert's Help, Describe the “placement new" and why would i use it? Discussions

Write discussion on Describe the “placement new" and why would i use it?
Your posts are moderated
Related Questions
#questioA Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a str

Project Description: I posted listings on a site that is like eBay but for video games. I post the similar listing many times daily, but need something that will let me post

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Car Rental System This system tracks cars in a rental company. Each car has a number (assume its plate number), type (small car, four wheel car), and status (rented, available). T

FUNCTIONS It refers to a subprogram that is meant to do a certain task. It is basically used to execute a set of operations and return information to the main or calling functio

Employ operator () instead of operator[]. While you have multiple subscripts, the cleanest way to do it is along with operator () instead of with operator[]. The reason is that

Problem : Change to palindrome A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your ta

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Explain protected, public, private in C++? These are three access spaecifiers in C++. - Public - Here data members and functions are accessible outside the class. - Pr

Here is a program that uses an inline function to compute and return the absolute value of its input argument. # include inline int abs(int x) {                  ret