memory questions

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Fukuda

memory questions

Post by Fukuda »

I have some memory questions, indeed some hazy parts to be clarified. My English is not so good so I ll try to explain it.At first where is the memory positions of this variables.

Code: Select all

char Var;
unsigned int *Var1;
unsigned int Var2;
I mean, are they in stack or what decides the memory positions of this variables. For instance are they in my binary code(As I know uninitialized variables dont take part in binary code)?

And their memory layout is another problem. Are their memory positions in the order of they defined? I mean:

Code: Select all

Var--->[0x0050]
Var1-->[0x0050+sizeof(Var)]
Var2-->[0x0050+sizeof(Var)+sizeof(Var1)]
Can I reach addr of 'Var1' from adding sizeof 'Var' to addr of 'Var'?

And a third point, for instance I need an array, an array of 0x1000 char. I decided to put it between 0x1000-0x2000. So in my C code I define this array as a pointer to that area, not as an array. I mean:

Code: Select all

unsigned char Var[0x1000];
not this way, but:

Code: Select all

unsigned char *Var = (unsigned char *)0x1000;
But as I mentioned above, this is an un-initialized variable and so the space is allocated for this array in my mind. Is that couse a problem? I wonder if computer puts another variable to that space before asking me?
Tim

Re:memory questions

Post by Tim »

Non-static local variables are on the stack.

Global variables and static local variables are in the executable: initialised variables go in .data, uninitialised variables go in .bss. Some compilers put read-only global variables in .text or .rdata.

You can't make too many assumptions about the layout of variables, particularly local ones. Indeed, some local variables might not exist in memory -- the compiler is free to put them into registers if it wants to. You can control the layout of structs, and to some extent you can control where global variables are placed by mentioning them in a linker script. But it's bad practice to rely on things like this.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:memory questions

Post by Pype.Clicker »

Tim Robinson wrote: N Indeed, some local variables might not exist in memory -- the compiler is free to put them into registers if it wants to.
For the sake of comprehensiveness, note that if you don't want the compiler to put a specific variable in a register, you can declare it volatile ... Computing the address of the variable will also prevent register substitution (but not necessarily caching).

If &x == &y+1, the compiler might not notice that *(&y+1)=0 actually updates x and thus the next reading access to x might reuse a register-cached value of x.

Listen to Tim-the-Wise and use structs/arrays when you want to enforce a specific memory layout ...
Post Reply