Skip to main content

Runtime Stack

Programs organize function calls using something called the Runtime Stack. The run time stack tracks all variables, parameters and function calls into Stack Frames. Each stackframe represents a function call. When a function is called, a stack frame is added to the top of the stack. When that function is finished, either because it has met the return statement or because it has reached the last statement within the function body, the stack frame is removed from the stack, making the stack frame of the calling function at the top.

Example:

Let us consider this would work with the following program.

#include <stdio.h>

int abacus(int);
int calculator(int);
int computer(int);

int main(void)
{

int result = abacus(0);
printf("result = %d\n", result);
return 0;
}
int abacus(int info)
{
int rc = 1; //rc stands for return code.
//it is just a name... you don't need
//to name it exactly this.

printf("abacus one\n");
rc += computer(info);
printf("abacus two, rc = %d\n", rc);
return rc;
}
int calculator(int data)
{
printf("calculator\n");
return data + 1;
}
int computer(int data)
{
int rc;
printf("computer one\n");
rc = calculator(data + 1);
printf("computer two\n");
return rc;
}

When the program starts We start with a stack frame for the main() function as this is what the operating system will call:

Runtime stack with main and variable named result


	int result = abacus(0);

This is a function call to abacus, which adds a new stack frame to the stack

add a stack frame for abacus() function


We do the first line of output from the abacus function. Next we face a function call to computer(0).

	rc += computer(info);

show output and note changes and upcoming call to computer()


As we are calling computer() function, we add a stackframe to the runtime stack. Here we are also performing the first output line from computer() function and getting ready to do the call to calculator:

frame for computer() function added, perform first line of output, show call to calculator


Here we have called the calculator() function and do the first line of output. Here, is a key concept, variables with the same name in different functions are not the same variables. here there are 2 data variables one in calculator() and one in computer(). abacus() and computer() each have a variable named rc

add a frame for calculator() call, note that data in frames are not the same


calculator() function is completed, so at this point, the frame is removed and a value (2 in this case) is returned to the calling function. The function call is replaced by the return value.

remove calculator() stack frame and make computer() frame top of stack


second line of output done in computer and return statement is reached after that. Thus computer stack frame gets removed returning a value to the abacus function. abacus() is now at the top of stack

remove computer() stack frame and make abacus() frame top of stack


rc is set in abacus, second line of output from abacus is done

update rc and do second line of output


remove the stackframe for abacus and return 3 to main()

remove stackframe for abacus and return 3


set result and do final line of output in main()

set result and do final line of output in main()