Create new C++ object at specific memory address?
Asked Answered
H

6

46

Is it possible in C++ to create a new object at a specific memory location? I have a block of shared memory in which I would like to create an object. Is this possible?

Hopkins answered 12/10, 2009 at 14:5 Comment(0)
S
75

You want placement new(). It basically calls the constructor using a block of existing memory instead of allocating new memory from the heap.

Edit: make sure that you understand the note about being responsible for calling the destructor explicitly for objects created using placement new() before you use it!

Scrim answered 12/10, 2009 at 14:8 Comment(13)
Thanks for a good answer to a good question. I learned something new today. I would have ended up fanangling C constructs into cpp world to accomplish this.Croydon
+1 for specifying that the Destructor has to be called explicitly, normally you don't care but when its has side effects besides releasing memory...Sanjay
I highly recommend reading all of Marshall Cline's C++ FAQ Lite several times. It is an invaluable resource.Scrim
Unfortunately the articile you point it is flawed and wrong. Using a char array like that is not a good idea. The problem is that an array declared like that has no guaranteed alignment. What you need to do is use a vector<char> Because the memory is dynamically allocated the memory will be correclty aliagened (iff the space allocated is equal to or larger than the size of the object (Check out the alighment properties for new in the standard))Foreknowledge
@Martin: interesting point... I'll have to dig into that one. Interestingly enough, the Standard uses the same example in clause 4 of 18.4.1.3. Then again, the example in the Standard lacks the necessary cast to void*... oh well.Scrim
@D.Shawley: I highly recommend not reading the FAQ to seriously. It is serioulsy flawed in many places, and often disagrees with commonly accepted best practices as espoused by Sutter/Alexan­dres­cu and Meyers.Foreknowledge
@Martin: Your comment about alignment is already noted in that FAQ, albeit without the mention of std::vector.Nest
@JAB: That's new. Rather than say it is dangerous why not describe a method that is guaranteed to work! The solution is just as simple as the version in the FAQ (as it is really really easy (use dynamic memory (A vector just being an example)).Foreknowledge
@LokiAstari: I know this is a very old comment but for future readers: do not attempt to create a std::vector in shared memory without using a custom allocator and really knowing what you are doing.Areaway
@Scrim 18.6.1.3 in the standard of 2011 year.Unstuck
The link is broken :(Myatt
Does STL use this in it's implementation of emplace?Outofdate
Please provide a code example right in your answer, including demonstrating how to call the destructor manually.Va
C
17

Yes. You need to use placement variant of operator new(). For example:

void *pData = ....; // memory segment having enough space to store A object
A *pA = new (pData) A;

Please note that placement new does not throw exception.

Caravansary answered 12/10, 2009 at 14:10 Comment(1)
is there any way to do something like placement new at compile time?Hydranth
G
1

if you want to allocate a lot of fine-grained objects, the best approach will be to use placement new in conjunction with some sort of a ring buffer. otherwise, you will have to keep track of the pointers aside from the object pointers themselves.

Gao answered 13/10, 2009 at 7:24 Comment(0)
V
1

Assuming you have a pointer to the memory location you're wanting to place an object at, I believe one can cast the pointer to a new type and then place an object at the location of that pointer. This is a solution which doesn't require new().

Given your memory:

// you would use the pointer you have to your allocation of memory char* mem_start = new char[1024]; // Now we have 1024 bytes to play with

One can cast it to a given type:

CustomType* object_ptr = (CustomType*) mem_start;

Lastly, you can construct an object there:

*(object_ptr) = CustomType();

Virgiliovirgin answered 14/9, 2018 at 19:29 Comment(0)
T
0

On Windows, MapViewOfFileEx and VirtualAllocEx allow one to specify a preferred virtual address. No guarantees though.

Tinnitus answered 27/2, 2017 at 10:44 Comment(0)
A
0
int *ptr=(int *)0x1234;
classname *objname = new (ptr) classname;

You can do it by simple doing this.

Aleris answered 19/1, 2022 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.