On many operating systems, the stack and heap begin on opposite sides of the virtual address space of the process and grow towards each other. This allows the stack to expand as much as possible without hitting the heap.
Suppose I have a program that causes a stack overflow. My real understanding is that this will cause the stack to grow uncontrollably to the heap and ultimately hit it. It's right? If so, how does the operating system detect that a stack overflow occurs? It seems that the OS will not be able to detect that the program was trying to use the virtual memory allocated for the heap as part of the stack, since they would be in adjacent memory areas.
I know that it depends on the operating system, but understanding the mechanism by which this happens on any operating system will definitely be useful. It annoyed me for a while, and I can not find any good explanations.
source
share