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?

DECODE, Smugglers are becoming very smart day by day. Now they have develop...

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

Algorithm, algorithm to prepare mark sheet of a student by inputing name,br...

algorithm to prepare mark sheet of a student by inputing name,branchcode,semester,register no,5 marks of students and total mark of student

Default value functions, Default Value Functions,  When declaring a functio...

Default Value Functions,  When declaring a function we can specify a default value for each parameter. This value will be used if that parameter is left blank when calling to the f

Program to find a greatest string: c - program , Program to find a Greatest...

Program to find a Greatest String: C - Program: Write a program find largest string by c program. int main( int argc, char *argv[] ) {     if( argc         {

Sums a sequence of integers, assume that the first integer read with cin sp...

assume that the first integer read with cin specifies the number of values remaining to be entered. that program should read only one value each time cin is executed .a typical inp

Procedure to add two numbers in a recursive manner, (a) Write a procedure c...

(a) Write a procedure called (add x y) that adds two numbers in a recursive manner. Specifically, note that x + y = (x + 1) + (y - 1) and x + 0 = x. (b) Using the substitution m

Types of linked list with the help of diagrams, Problem: (a) Describe ...

Problem: (a) Describe a linked list. (b) Explain the three different types of linked list with the help of diagrams. (c) Give two advantages and two disadvantages o

Palindorme, program coding for the conversion of string to a palindrome

program coding for the conversion of string to a palindrome

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