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?

#padovan string in c , #padovan string in java   program in java /...

#padovan string in java   program in java // aakash , suraj , prem sasi kumar kamaraj college program 1 : package test.padovanstring; public class PadovanStrin

C program to create, Aim: To implement a program to create, update & displ...

Aim: To implement a program to create, update & display account & admin record of person using display account & admin records of person using virtual base class.. Code:

Struct items, how to make a program this struct items/product

how to make a program this struct items/product

If statement, Write a program to input two integers. If they are equal disp...

Write a program to input two integers. If they are equal display "The numbers are equal". Otherwise display a message if the first is a multiple of the second or if it is not a mul

Need cron job parsing json from api, Need Cron Job Parsing JSON from API, I...

Need Cron Job Parsing JSON from API, Inserting in to DB Project Description: The Project is to prepare a Cron Job with an adjustable interval in seconds and milli seconds. Cr

Program to create a class and store student information, Develop a Student ...

Develop a Student class that has the following header file: #ifndef STUDENT_H #define STUDENT_H #include #include #include using namespace std; class Stu

How do i change an integer to a string?, A: The simplest way is to use a st...

A: The simplest way is to use a stringstream: #include #include #include using namespace std; string itos(int i) // convert int to string { stringstream s;

Area under curve, Write a program to find the area under the curve y = f(x)...

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

About, How can I learn all programming languages.?

How can I learn all programming languages.?

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