Possible Duplicate:
What and where are the stack and heap
My installation of Ubuntu has a default stack size limit of 8 MB. But I am curious as to why we need to restrict a user program's stack size. The same program can use all of its 4 GB (for a 32 bit program) addressable space via malloc/mmap etc. So why do we need a stack size limit? Why can't the stack grow till it almost meets the heap?
If the stack would be allowed to grow arbitrarily large, these errors (like infinite recursion) would be caught very late, only after the operating systems resources are exhausted. This is prevented by setting an arbitrary limit to the stack size.
The stack size limit is the maximum size of the stack for a process, in units of 1024 bytes. The stack is a per-thread resource that has unlimited hard and soft limits. Set or display the CPU time limit. The CPU time limit is the maximum amount of CPU time (in seconds) allowed for the process.
Yes, stack is always limited.
The stack size might increase because: Each register push takes 4 bytes of memory in ARM, while in 16-bit or 8-bit each register push take 2 bytes or 1 byte. In ARM programming, local variables are often stored in the stack, while in some architectures local variables might be defined in a separate data memory area.
In fact the stack does grow more and more. It doesn't need to start very big since in the general case, it doesn't need to be very big. Having it as very big results in a wasteful memory footprint.
I'm not 100% sure as to how the stack is implemented on Linux but on Windows, a large amount of space is reserved for the stack. This amount can be set in compiler options (you may want a larger stack for deeply recursive programs). At runtime, the stack can be extended dynamically via a guard page system. At the end of the stack there is a guard page which when hit will extend the stack by an extra page and push the guard page forward by one.
Stack probing is another interesting and related concept. So your question of 'why can't the stack grow till it almost meets the heap?' The stack does grow but since most of the time having a huge stack is likely an undesired side-effect of a bug, the reserved size will not be huge (although this is settable).
This article is very interesting and relevant to your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With