16

Can I Allocate a specitic memory address using pointers in c++ ?

For example: Allocate This memory address 25D4C3FA and put 4 in it.

faressoft
  • 19,053
  • 44
  • 104
  • 146

4 Answers4

25

Allocating a specific address in your process's address space is a bit tricky and platform-specific. On Unix systems, mmap() is probably the closest you're going to get. The Windows equivalent is VirtualAlloc(). There are, of course, no guarantees since the address might already be in use.

Writing to a specific address is trivial:

char *p = (char*)0x25D4C3FA;
*p = 4;

I assume you have good reasons to want to do that.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 6
    It's worth adding that on Windows, one would want to call `VirtualAlloc`. –  Apr 28 '12 at 14:55
  • Can you give me an example on VirtualAlloc to allocate this address 25D4C3FA – faressoft Apr 28 '12 at 15:12
  • @faressoft: I think we'd be in a better position to help you if you explained where you're going with this. – NPE Apr 28 '12 at 15:14
  • I want to know if I can allocate the same address of 'a' in this example http://ideone.com/2ZJ57 to see why every time I call the function 'a' have the same address http://ideone.com/2ZJ57 it doesn't deallocated or what what happen if I allocate this address ! – faressoft Apr 28 '12 at 15:20
  • 4
    Take note that pointer is not integer type (unlike char datatype , which is naturally a 8-bit size integer , that's the reason why you can do char a = 65; ) , so you can't directly do this char *p = 0x25D4C3FA; , there's one method to write to specific address , that is by doing this , char *p = (char *)0x25D4C3FA; – caramel1995 Jun 01 '12 at 18:15
  • @KerrekSB: OK, added an explicit cast :-| – NPE Aug 13 '14 at 05:41
  • But how on ARM Cortex-M Microcontrollers? – mohammadsdtmnd Dec 05 '22 at 09:35
  • I get the error *Exception thrown: write access violation. p was 0x25D4C3FA.* for the second line on my Visual Studio! – Franky Apr 12 '23 at 10:52
3

In Windows, yes.

pseudo-code:

Pointer desiredAddress = 0xD0000000;

//allocate 1 KB at our desired address
Pointer p = VirtualAlloc(desiredAddress, 1024, 
      MEM_COMMIT | MEM_RESERVE,  
      PAGE_READWRITE);
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
0

Assuming that by allocate you actually mean access,

You can, but if the address is invalid or not accessible then deferencing the address will result in Undefined Behavior.
So it is not a good idea to do so.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • No, in standard C++ you can't. You can set a pointer to 0x25f4c3fa and try to use it, but this is not "allocating". – Matteo Italia Apr 28 '12 at 14:53
  • @MatteoItalia: The OP means setting a pointer rather than allocating here if you read the complete q. – Alok Save Apr 28 '12 at 14:55
  • 5
    The question is quite ambiguous, but he specifically says "allocate" several times. – Matteo Italia Apr 28 '12 at 14:56
  • @MatteoItalia: Since as you already said OP probably doesn't know what S/He wants the argument over terminology used it pointless. – Alok Save Apr 28 '12 at 14:58
  • 1
    @AIs Nobody said that the OP doesn't know what he/she wants - just that we don't. Either way, you should always assume that people mean what they say (and the OP said "allocate") unless you have good reason to suspect they meant something other than what they said. – sepp2k Apr 28 '12 at 16:43
  • 1
    And if you do, you should specifically mention that, so that it's clear that you're answering a different question than the one that was asked. The way your answer is now, someone reading it might very well be led to believe that you can allocate a specific memory address using pointers in standard C++. If you had started your answer with "I'm assuming that by 'allocate' you actually meant 'access'", it would have been unambiguous. – sepp2k Apr 28 '12 at 16:45
  • @sepp2k: *Nobody said that the OP doesn't know what he/she wants* it was said in the comment above mine and was stealth edited(as I see now), so those are not my words & The way I see it I answered the Q that was asked clumsily.I will add that assumption to the answer if it makes you happy. – Alok Save Apr 28 '12 at 16:57
0

You can request a specific address through VirtualAlloc on Windows, and I expect other operating systems do the same, but there are no guarantees and no platform-independent means.

Puppy
  • 144,682
  • 38
  • 256
  • 465