Here's a layout of memory in c for short and int variables (on 64bit OS compiled with -m32 and -m64 switch). In actual program variables are declared in reverse order than shown on images.
For this image, upper table, declaration was short, short, int, int and for the lower table declaration was: int, short, int, short.
This is how c would layout a long and int combination.
This is how c would layout all different kind of variables. (variable long answer is declared in separate function).
Conclusion: C stores a variable on an address that is divisible by size of that variable.
I.e.
- if a variable is type of double it is stored at locations divisible by 8 => 4424, 4432, 4448...
- if a variable is type of int it is stored at locations divisible by 4 => 4556, 4552, 4548...
- if a variable is type of int it is stored at locations divisible by 2 => 4558, 4556, 4554...
No comments:
Post a Comment