[Solved] Why aren’t consecutively-defined variables in increasing memory locations?


There’s no reason for them to be, so the compiler does what’s best for itself.

Complex compiler optimizations and efficient and linear memory layout don’t all go together, so linear memory layout was sacrificed.

When you get to hashtables, you will learn how the most efficient algorithms lead to repeatable pseudo-random output order. The symbol names are loaded into a hashtable when compiling; and the function memory space could laid out by iterating over the global symbol table, which is therefore in some arbitrary order.

In general you will find this is true. Whenever the order isn’t specified it will be let to fall however, and when algorithms are non-trivial, however isn’t simple anymore.

Don’t ever depend on order of symbols in memory layout. It’s not allowed.

4

solved Why aren’t consecutively-defined variables in increasing memory locations?