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

Assignment Help:

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


Related Discussions:- Describe the “placement new" and why would i use it?

How to creat a file herachy for stream class, Ask questionhow to creat a fi...

Ask questionhow to creat a file herachy for stream class #Minimum 100 words accepted#

Arithmetic operation, a program that declares and prompts the user to enter...

a program that declares and prompts the user to enter two integer and performs the five arithmetic operations

Decode the Code, Smugglers are becoming very smart day by day. Now they hav...

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

I need poker room auto seat program, Project Description: I want a scrip...

Project Description: I want a script for an online poker site. It needs to be scanning all the required tables and when the table meets certain criteria it should take a seat at

Explain the bit fields portable or not, Explain the  bit fields portable o...

Explain the  bit fields portable or not? - No, Bit fields aren't portable. - As Bit fields can't span machine words and number of bits in a machine word is different on diff

Prepare data from fixed ocr areas scanned, Prepare data from fixed OCR area...

Prepare data from fixed OCR areas scanned. Project Description: Project is to design software which can use character recognition only on certain areas of scanned pdf's/ jpeg

Padovan string, padovan string program 1 : package test.padovanstring; ...

padovan string program 1 : package test.padovanstring; public class PadovanString {     public int stringOccurrences(int n, String str){        if(n >= 40)     return -

Identify and briefly explain the objects and methods , You are to develop a...

You are to develop a Clinic System. 1. Identify and briefly explain the objects, classes, methods and attributes for the Clinic System. 2. Draw a use case diagram to represen

3/15/2013 5:37:27 AM

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.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd