Does it always happen that stack always grows downwards & heap grows upwards?
If its so, then how does OS keeps the heap area protected from the interference of the stack & vice-versa?
If its not, then what factors affect it? OS version ? Compiler? Anything else??
Does it always happen that stack always grows downwards & heap grows upwards? If
Share
Yes this is usually the case. The implementation details of this is managed by the OS and varies widely. To make a very simple example, assume that the stack starts on a higher address in memory compared to the heap which starts on a lower address. If you keep track of the top of the heap and the top of the stack and you put more data on the heap and more data on the stack they will eventually meet somewhere inbetween because as you said the stack grows downwards and the heap upwards. When this occurs the memory is out so all you need to keep track of is when these two have collided.
Now for a real operating system this is often not this simple because each process has it’s own stack and some have their own heap and we are maybe working on virtual memory addresses where the actual physical placement could be anywhere. You can assume that each heap and stack is given an address and a limit which form the boundries of the maxiumum amount of memory each process can utilize. If this limit is reached the OS need to tell the process either that it is out of memory or try to find more memory to allocate for the out of memory process.