High Address Preference in Stack

Jan 12, 2021 at 4:30pm
Is it true that structures are stored in stack starting from the highest hexadecimal address and in descending order? If yes, is there a good reason?
Jan 12, 2021 at 4:38pm
The reason is to give the maximum amount of space between the stack and the heap so that they don't run into each other. The stack grows downwards and the heap grows upwards. (Presumably it could've been the other way around.) The overall memory layout of a program is something like:

High:  stack (growing downwards)
         |
         ▼


         ▲
         |
Mid:   heap (growing upwards)
Low:   globals (unitialized (zeroed))
       globals (initialized)
       program text

Jan 12, 2021 at 5:08pm
Thanks dutch. Neat response! It makes sense that the "heap" grows upwards because in real life, heaps do pile "up" :-) And that's a good mnemonic now, so I know stack grows the opposite direction.
Last edited on Jan 12, 2021 at 5:09pm
Topic archived. No new replies allowed.