Kaiyakha
Kaiyakha

Reputation: 1913

Are stack and heap variably sized?

In accordance to many sources, stack and heap are memory regions devided by empty space. As far as I got this, the stack and the heap grow towards each other as the program is running.

Does that mean that the stack and the heap are both of variable size? If they are, can one of them occupy the same addresses the other occupied during the previous run (suppose the two runs took place at the same memory addresses)?

If they are of variable size, I guess stack overflow occurs when the stack tries to take what belongs to the heap. If not, a stack bound is just what it is and placed somewhere in the middle of the initially empty space between the two regions of memory.

A bit of clarification: by the variability of size here I mean changing of the reserved space. That is, here I imply that if stack size is variable, the reserved for it space changes (like when you change size of an array using realloc, but that is just for comparison); stack overflow is then occurs by the stack hitting some bound, be it another region like heap or something else. If it is not variable, the reserved space stays the same, and stack overflow is merely caused by running out of the reserved space. And I also wonder if heap is variably sized too.

Upvotes: 0

Views: 95

Answers (1)

doron
doron

Reputation: 28872

The stack must be contiguous in address space. The heap has no such constraint. Anything beyond that depends how a given platform lays out the memory.

If your address space cannot have holes, a simple layout for an embedded device can be: from address n and below is stack (stacks tends to grow down) and from n and above can be used for heap. Static is then some high address and code is in ROM.

Linux and Windows run on platforms with an MMU so have a lot more options with regards memory arrangement.

Upvotes: 1

Related Questions