Is it possible to allocate memory in a specific address? Is there a function to solve this problem?
-
3What operating system, what is the reason for doing this? – Michael Aug 12 '14 at 16:07
-
You can use placement new, but it has to be memory you already own. – Luchian Grigore Aug 12 '14 at 16:08
-
1http://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new – Luchian Grigore Aug 12 '14 at 16:09
-
if I use new operator, i dont decide the memory address... – user3793107 Aug 12 '14 at 16:09
-
Yes. In fact, Microsoft Office does this. – Mike Christensen Aug 12 '14 at 16:10
-
2Why do you feel the need to allocate memory at a specific address? – David Heffernan Aug 12 '14 at 16:14
-
The question is subtly nonsensical: The notion of "a specific address" does not exist. Valid addresses are addresses of objects. Addresses don't exist disconnected from existing objects. – Kerrek SB Aug 12 '14 at 16:57
-
Every byte in your RAM have an address. If addresses doesn't exist, how do you allocate objects in the first place ? – Aug 12 '14 at 17:28
-
@Nax: You got it the wrong way round: First you allocate an object, and *then* you can take the object's address and thus get a valid address. – Kerrek SB Aug 12 '14 at 18:08
-
@KerrekSB: No. First, you get an address, and then you modify the bytes starting at this address to create your object. – Aug 12 '14 at 18:56
-
@Nax: Not in C++ :-S – Kerrek SB Aug 12 '14 at 20:13
-
@KerrekSB The rules can be different on unhosted implementations (bare metal) as well as some OS APIs allowing this also. – Vality Aug 12 '14 at 22:02
-
@KerrekSB: How do you create objects on the heap then? Do you imply that every object creation imply a copy? Do you realize how inefficient it would be? – Aug 13 '14 at 08:33
-
@Nax: `auto * p = new T;` See, no addresses needed. You get an address *returned*, but that's *after* you create the object. – Kerrek SB Aug 13 '14 at 09:04
-
@KerrekSB: Yes, because you used a regular new. But what new is doing under the hood is calling `malloc` (virtually every implementation does), get a free address, create your object at this address, then return the address (so you can refer your object). – Aug 13 '14 at 09:06
-
@Nax: You also don't need an address to call `malloc`. Again, you **get** an address after the memory has been allocated. I repeat: The only valid addresses are those that you *get* from the system. You cannot *prescribe* addresses. Addresses do not exist until you are told them. – Kerrek SB Aug 13 '14 at 09:37
-
@KerrekSB: I never said you needed an address to call `malloc`. What I said is addresses are not just identifiers for objects, they are related to the physical place where objects are located in memory (with a level of abstraction, because of pagination). Unallocated addresses are still addresses, and, as long as they are mapped, you can still manipulate them. In user-land, that will trigger a SIGSEGV or a SIGBUS, but it's perfectly valid in kernel-land. – Aug 13 '14 at 09:52
-
@Nax: My original comment was abbreviated, possibly to a fault: What I meant is that valid addresses are either address of objects or return values from library calls such as `malloc` or one of the `operator new` allocation functions. My point stands: There is no such thing as a prescribed "specific memory address" in the OP's sense in standard C++. – Kerrek SB Aug 13 '14 at 10:05
3 Answers
Sure. You can use something called placement new
.
It's useful if you're targeting mobile platforms with very tiny RAM, or when you're writing a memory pool.
Please note that, unless your executable have special privileges (a kernel extension for example), you MUST own the memory you're allocating at.
An (useless) example:
// Assuming you can access and read/write at 0x1234ABCD
unsigned char* ptr = reinterpret_cast<unsigned char*>(0x1234ABCD);
// Allocate a std::string at ptr
std::string* foo = new(ptr) std::string;
To allocate at a specific address, you'll need to use an OS specific API. However, if you have the allocated space and address you can use the new with placement operator to construct an object at an address within the allocated space. The allocated space could also be from a call to new on a char array, or something simple like that.

- 820
- 6
- 12
-
1Some platforms, such as embedded systems, can allocate memory without an RTOS, because, many don't use an OS. – Thomas Matthews Aug 12 '14 at 16:35
I only know of a way to do this under Linux, you need to use the mmap call which allows you to request a specific address as so:
void *foo = (void *)0xDEADBEEF;
size_t MyLength = 1024;
void *bar = mmap(foo, MyLength
, PROT_READ | PROT_WRITE | MAP_ANONYMOUS | MAP_FIXED, MAP_PRIVATE, -1, 0);
Note this will fail if the address is already in use or is not a multiple of the page size (either 512 or 4k bytes)

- 6,577
- 3
- 27
- 48