The stack is a region of a process's address space used primarily to manage function calls. Each thread within a process typically has its own stack.
- How it works: The stack generally follows a Last-In, First-Out (LIFO) structure, much like a stack of plates. When a function is called, a stack frame may be created for that function. When the function returns, its stack frame is removed.
- What it stores: Stack frames can contain information associated with function calls, such as local variables, function arguments, return addresses and saved register values. Some of this information may instead be stored in CPU registers depending on the architecture, calling convention and compiler.
- Speed: Allocating and deallocating stack memory is generally very fast because it can often be performed by adjusting the stack pointer. Stack memory also commonly benefits from good memory locality. Accessing stack memory, however, is not inherently faster simply because the data is stored on the stack.
- Management: Stack memory is normally managed automatically as functions are called and return. The compiler generates instructions that establish and remove stack frames as required, while the processor uses registers such as the stack pointer to keep track of the current stack position.
- Limitations: A thread's stack has a limited amount of address space available to it. Excessive stack usage, such as very deep recursion or very large local variables, can exceed this limit and result in a stack overflow.
Stack Memory and the Operating System
The stack is not a separate type of physical memory. Like the heap, it is a region of a process's virtual address space. The operating system manages the virtual address space and the mapping of its virtual memory pages to physical memory.
When a process is created, the operating system establishes the initial virtual address space required by the process, including a stack for its initial thread. When additional threads are created, each thread is typically given its own stack.
The operating system does not normally create and remove a stack frame every time a function is called. Stack frames are primarily managed by the executing program according to instructions generated by the compiler and the calling convention. The operating system instead manages the memory in which the stack exists.
A stack may be given a maximum size or a reserved range of virtual address space. Depending on the operating system and runtime, additional pages can be made available as the stack grows. Through virtual memory and paging, the physical memory backing the stack does not need to occupy one continuous region of physical RAM.
Operating systems can also use a protected region known as a guard page near the boundary of a stack. If the stack grows into this protected region, the memory access generates a fault. This can help the operating system or runtime detect that the stack has exceeded its permitted range, resulting in a stack overflow or process termination.