A stack is a memory location allocated to your OS by the operating system. After it is allocated, the OS sets the register (on x86, it esp ) to where the stack is located, and then runs your program. Compilers know that if they use the value in this register as the stack pointer, everything will be fine. Then they do whatever they want to do with it. The OS simply allocates a zone. He does not care about how he was used after.
The OS does not know if your program will mainly use the stack or heap. However, since most programming languages โโuse the stack in one way or another, he knows that he must allocate one. For example, Java stores its objects on the heap, but most JVM implementations will use the stack to support call frames (and primitive local variables), so it also needs a stack.
source share