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?
6 Answers
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!

- 58,213
- 10
- 98
- 113
-
4Thanks 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. – San Jacinto Oct 12 '09 at 14:19
-
9+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... – Matthieu M. Oct 12 '09 at 14:22
-
2I highly recommend reading **all** of Marshall Cline's C++ FAQ Lite several times. It is an invaluable resource. – D.Shawley Oct 12 '09 at 14:23
-
2Unfortunately 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
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)) – Martin York Oct 12 '09 at 18:37 -
@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. – D.Shawley Oct 12 '09 at 20:41
-
2@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/Alexandrescu and Meyers. – Martin York Oct 12 '09 at 23:01
-
@Martin: Your comment about alignment is already noted in that FAQ, albeit without the mention of std::vector. – JAB Jul 15 '10 at 13:15
-
@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)). – Martin York Jul 15 '10 at 14:18
-
@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. – Zan Lynx Feb 25 '12 at 03:06
-
@D.Shawley 18.6.1.3 in the standard of 2011 year. – bartolo-otrit Jan 01 '14 at 20:23
-
4The link is broken :( – Christian Aug 27 '15 at 10:04
-
1Does STL use this in it's implementation of `emplace`? – Jon Deaton Dec 24 '17 at 01:00
-
Please provide a code example right in your answer, including demonstrating how to call the destructor manually. – Gabriel Staples Sep 12 '20 at 16:45
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.

- 26,717
- 34
- 141
- 196
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.

- 325
- 2
- 7
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();

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

- 4,104
- 2
- 29
- 45
int *ptr=(int *)0x1234;
classname *objname = new (ptr) classname;
You can do it by simple doing this.

- 2,445
- 11
- 18
- 28

- 35
- 8