In computer science, a call stack is a stackdata structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or machine stack, and is often shortened to just "the stack". Although maintenance of the call stack is important for the proper functioning of most software, the details are normally hidden and automatic in high-level programming languages. Many computer instruction sets provide special instructions for manipulating stacks.
Functions of the call stack
As noted above, the primary purpose of a call stack is to store the return addresses.
Depending on the language, operating-system, and machine environment, a call stack may serve additional purposes.
Structure
A call stack is composed of stack frames (also called activation records or activation frames).
Each stack frame corresponds to a call to a subroutine which has not yet terminated with a return. For example, if a subroutine named DrawLine
is currently running, having been called by a subroutine DrawSquare
, the top part of the call stack might be laid out like in the adjacent picture.
Call stack layout for upward-growing stacks
The stack frame at the top of the stack is for the currently executing routine. The stack frame usually includes at least the following items (in push order):
- the arguments (parameter values) passed to the routine (if any);
- the return address back to the routine's caller (e.g. in the
DrawLine
stack frame, an address intoDrawSquare
's code); and - space for the local variables of the routine (if any).