4 ms·
Stacks are small because every thread in the system has to have one, it's that simple.
by jemfinch 15y ago
Stacks are small because every thread in the system has to have one, it's that simple.
- quotemstr 15y agoWho said stacks had to be contiguous and allocated ahead of time?
- emillon 15y agoHow would you manage non-contiguous stacks ? ie, how would you know (upon return) where is the caller's stack frame is ?
- stephencanon 15y agoYou would keep a frame pointer, which would tell you where to find the caller's stack frame. Most systems already do this, to aid debugging and tracing tools, to support variable-length stack allocations, or for any number of other reasons.
- exDM69 15y agoThe stack is only contiguous in the virtual address space. The physical address space is a different beast. The operating system allocates a big chunk of virtual address space for the stack but doesn't allocate the physical memory to fill that. When an app wants to read or write from the unallocated part of the stack, a page fault interrupt is generated and the operating system allocates physical memory frames and assigns them to the virtual memory addresses of the stack. When the thread/process is interrupted for servicing, the operating system checks the value of the stack pointer register (rsp in x86_64) and frees the physical memory corresponding to the virtual addresses above that in the stack. So having a large stack will only consume virtual address space which we have plenty of (2^48 bytes in most x86_64's) but will not consume physical memory, which is a more limited resource.