Stack for Local Variables

Learn how the local variables and the stack are related.

Introduction

We’ll look at how the compiler places variables on a stack. Before doing that, we need to learn one more difference between regular stack data structures and the stack used for variables.

Stack grows downward

With a regular stack, we keep adding elements at the top, and the stack becomes taller and taller. We say that the stack grows upwards.

However, for historical reasons, the stack used for variables grows downwards. This behavior doesn’t have any implications for us. But, for accuracy, we’ll represent the stack as growing downwards, not upwards.

Get hands-on with 1400+ tech skills courses.