Function Call Stack and Activation Records
To
understand how ‘C’ executes function calls, we first need to consider a data-structure
(i.e., collection of related data items) known as a stack. A stack is analogous
to a pile of dishes.
To
understand is place on the pile, it’s normally placed at the top (referred to
as pushing the dish onto the stack). Similarly, when a dish is removed from the
pile, it’s always removed from the top (referred to as popping the dish off the
stack).
Stacks
are known as last-in first-out (LIFO) data structures-the last item pushed
(inserted) on the stack is the first item popped (removed) from stack.
When a program calls a function, the called function must know how to return to its caller, so the return address of the calling functions is pushed onto the program execution stack (sometimes referred to as the function call stack).
If
a series of function calls occur, the successive return addresses are pushed
onto the stack is last-in, first-out order, so that each function can return to
its caller.
The
program execution stack also contains the memory for the local variables used
in each invocation of a function during a programs execution.
This
data, stored as a portion of the program execution stack, is known as the
activation record or stack frame of the function call.
When
a function call is made, the activation record for that function call is pushed
onto the program execution stack.
When
the function returns to its caller, the activation record for this function
call is popped off the stack and those local variables are no longer known to
the program.