Call stack starts at bottom or top

functionsstack

A stack is something that piles bottom-up.

Hence a call stack adds new items on the stack when functions are called with items being removed from the stack as each function ends until the stack is empty and then the program ends.

If the above is correct, why do people refer to control moving "up" the call stack? Surely control moves down the call stack until it reaches the bottom.

Best Answer

There are two possible reasons for this usage:

  • In the context of exceptions, the control moves to the calling function/method, and this call hierarchy is typically visualized with the main method on top and method calls forming a hierarchy downwards, with a decreasing level of abstractions. In this hierarchy, an exception moves upwards.

  • The actual program stack in a normal x86 application is inverted, i.e. it grows downwards. The PUSH / PUSHW / PUSHD machine code instructions decrease the stack pointer. Other architectures may share this model.

Related Topic