1

I've been learning about C recently, and I've encountered something which I can't seem to find a good answer to.

In my code I might have:

struct randomStruct a;
struct secondStruct array[5];
struct structyStruct q = { 17, "Hey yo", {123,123}};

But in memory, they might be stored as:

q, a, array

What I mean here is that q's adress in memory is lower than a's, which is lower than array's.

What determines the order in which variables are saved in memory?

(If you need more information/actual runnable code, please let me know)

Thanks in advance!

bjrnt
  • 1,782
  • 3
  • 20
  • 32

4 Answers4

4

What you are seeing happens because of the direction of the stack growth.

The standard says nothing whatsoever about addresses of automatic variables. Thus it's not required that a relation between variables' addresses exists.

Community
  • 1
  • 1
cnicutar
  • 164,886
  • 23
  • 329
  • 361
  • 1
    The standard says nothing about the order of storage. Indeed, changing compiler optimization flags can change the order! – Raymond Chen Oct 24 '11 at 14:58
  • Thanks! I'm vaguely familiar with that. However, it doesn't explain why it's 'q, a, array' (I thought it'd be 'q, array, a' or 'a, array, q' depending on stack direction, but not 'q, a, array'). – bjrnt Oct 24 '11 at 15:08
2

The C standard specifies the memory order of items within a struct, and many (perhaps all) implementations explicitly specify the memory order of parameters to variadic functions, but the order of automatic and static variables should be regarded as something which a compiler may arbitrarily or randomly arrange without rhyme or reason. Many compilers use a hash table for identifiers, and at least some compilers would arrange variables according to their hash codes, which made variable placement effectively random.

Further, some processors are designed so that certain variable placements will yield faster execution than others. For example, a compiler for a certain microcontroller may determine that placing two variables within 256 bytes of each other will allow it to load the address of one into a register and then use short-displacement register-relative addressing to access both; even if today's compiler wouldn't make that optimization, there's no reason to believe tomorrow's won't.

If you care about the relative arrangement of variables in memory, then you must either mark the variables in a vendor-specific way (typically using #pragma directives) to control how they are placed in memory, or else place all variables of interest into a struct. In some cases, it may be helpful to use #define macros to allow the struct items to be referred to by what look like variable names; the biggest caveat with doing this is that #define macros do not obey any normal rules of scope; consequently, one must be certain that the "variable" names are not used as any sort of identifier anywhere in the program.

supercat
  • 69,493
  • 7
  • 143
  • 184
2

In the C language, separate objects (i.e. objects which are not part of the same structure or array) do not have an order in memory. Comparison and subtraction of pointers are defined only for pointers to elements of a common array.

R.. GitHub STOP HELPING ICE
  • 195,354
  • 31
  • 331
  • 669
1

All those items are declared on the stack, which is why you are seeing this.

See the wiki on stacks.

http://en.wikipedia.org/wiki/Stack_(abstract_data_type)

Then checkout this on memory layout. Should answer your question.

http://discussknowhow.blogspot.com/2008/08/memory-layout-of-c-program-stack-wise.html

logancautrell
  • 8,762
  • 3
  • 37
  • 50