Tech

Stack

Like a can of Pringles chips, it is a storage box where the last one put in is always the first one you take out.

  • 2 min read
  • 1 figures
  • Verified 2026-10-12

Definition

A stack is a fundamental way computers organize data (a data structure). Like stacking books inside a narrow box, newly added items go right on top, and retrieving data always begins with the newest item sitting at the very peak.

Think of a Stack of Plates in a Kitchen

In a busy restaurant kitchen, washed plates are stacked tall. When a clean dish is added, it naturally goes right onto the top. And when someone needs a plate, they never yank one from the bottom—they simply lift the top one off.

Computers store and retrieve data the exact same way using a structure called a stack. The last piece of data put in is the first one to come out. In computer science, this principle is called LIFO (Last In, First Out).

You rely on stacks every single day without noticing. The 'Back' button in your web browser and the 'Undo' shortcut (Ctrl+Z) in document editors work on this exact mechanism.

How Computers Remember What to Do Next

Imagine you are studying, your phone rings, and while chatting, someone rings your doorbell with a delivery. You answer the door first, then wrap up your phone call, and finally return to your desk. You handle interruptions in reverse order.

Computers execute programs in this exact pattern by calling functions (reusable blocks of code). When a running program pauses to run another task, it saves its current position onto a stack. This specialized workspace is called the call stack.

Once the nested subtask finishes, the computer pops the top record off the stack and returns smoothly to where it left off. Thanks to the stack, computers handle deeply layered operations without ever losing their place.

The Fine Print: When the Container Spills

Adding data to a stack is called a push, and taking it out is called a pop. Because both actions happen exclusively at the top edge, adding and removing items is exceptionally fast—the system only ever tracks that single top position.

However, a stack's memory space is finite. If a coding error causes a program to call itself endlessly without stopping (an infinite recursion loop), bookmarks pile up until they exceed memory boundaries.

This crash is known as a stack overflow. It is so famous in software engineering that the world's most popular developer forum takes its name from it. When this happens, the operating system abruptly stops the program to shield the rest of your system from crashing.

Common misconceptions

Myth

You can easily pluck out any data item sitting in the middle of a stack.

Fact

A stack only allows access to the item at the very top. To retrieve an item tucked in the middle or at the bottom, you must first remove all the items resting above it.

Where you meet it

  1. Clicking 'Back' in your web browser visits previously viewed pages in reverse order, starting with the most recent one.

  2. Pressing 'Undo' (Ctrl+Z) in a document rolls back edits one by one, starting from the very last action you performed.

In one sentence

A LIFO (Last In, First Out) storage structure where the most recently added item is always the first one retrieved.

Created 2025-11-11 Updated 2026-10-12 Verified 2026-10-12