Terminology – Proper Usage of Up/Down the Stack

stackterminology

What is the convention about describing the direction of the stack, i.e., about what the words up, down, top, and bottom mean?

I noticed that with stack data structure API, top usually to refer to the most recently added item. I never saw high level language or library documentation refer to the newly added item in the stack as anything other than "top".

On the other hand, discussions about the program call stack often use the opposite convention, referring for example to the exceptions propagating up the stack (from the currently executing function towards the main function). Does the convention vary depending on the language, or some other factor?

To clarify: my question is only about the usage in the context of high-level language programming – not the direction in which the CPU/OS grows stack in physical memory (which varies by platform).

Best Answer

You push items onto and pop them off of the top of the stack, regardless of how it is laid out in memory.

The stack terminology is intended to mirror a physical stack (of plates, in particular):

Stacks are often described by analogy to a spring-loaded stack of plates in a cafeteria. Clean plates are placed on top of the stack, pushing down any already there. When a plate is removed from the stack, the one below it pops up to become the new top (Wikipedia, Stack History)


As it pertains to exceptions, strictly speaking, an exception propagates down the stack, from the most recently called function to the least recently called function. Google "exception propagation," and one of the first few results shows it like this:

enter image description here

However, when someone says "propagates up the stack," we understand what they mean. So, we don't fuss over it. Whether it's up or down in their heads isn't as relevant as whether they ultimately mean "most recent" to "least recent" (or "in opposite order of invocation").