0

I must declare some global variables which have to be stored every time the program runs at the same memory address. for example :

int a[10];

If I run this program multiple times in visual studio then the address of array a keeps on changing. How to get the fixed RAM address ?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    You can't. It's simply impossible. And why *would* you want that anyway? – Some programmer dude Jan 06 '14 at 08:49
  • it´is possible but its not clear in that discussion http://stackoverflow.com/questions/15267001/how-to-store-a-variable-at-a-specific-memory-location – user3164707 Jan 06 '14 at 09:02
  • The question and accepted answer in that link is about *embedded* systems, where you can do this. On Windows, Linux or OSX (especially if the system uses address-space randomization) or other operating systems which uses virtual memory you can't do it like that. – Some programmer dude Jan 06 '14 at 09:06
  • To help you formulate your question better, you might want to read about [the XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). With this I mean that instead of saying "I have this solution (to a problem I won't mention), how do I make it work?" you should tell us what problem you are having, and what solution(s) you have tried to make it work. Also read [the Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist). – Some programmer dude Jan 06 '14 at 09:09
  • thank you very much for the reply. I changed the randomization address into NO in my ide (i.e linker section) and ill get a fixed address of the variable and later it will get changed automatically. I am declaring a global variable of specific size and using that address to store a specific variable in a file and later to send a specific value to that address – user3164707 Jan 06 '14 at 09:15

2 Answers2

1

you can't really get a fixed RAM address (see virtual memory), what you can do is declare the variable as static, which will give you a constant virtual address.

hcf
  • 148
  • 5
1

There are two mechanisms which prevent what you try to do here.

  1. Virtual memory
  2. Address randomization

With address randomization the image would be loaded at different addresses every time. If you really mean a hardware RAM address, then you need to do this from kernel space anyway as you don't know which physcial address a page get assigned to.

If you declare a global variable it will always have the same offset when the program is loaded as long as you don't change the program. If this is enough, then you can use this registryx setting to disable address randomization

HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\MoveImages

However, this would obviously only work on machines where you change this setting. Also if you recompile the program, then your memory layout may change and the variable may have a different address.

The question of course is, what you want to achieve with this?

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • thank you very much for the reply. I changed the randomization address into NO in my ide (i.e linker section) and ill get a fixed address of the variable and later it will get changed automatically. I am declaring a global variable of specific size and using that address to store a specific variable in a file and later to send a specific value to that address. – user3164707 Jan 06 '14 at 09:09