liveBook · Manning

Stack

As explained in Latency

A stack is a memory region that supports function execution by storing call state, registers, return information, and local data.

A stack is a memory region that supports function execution by storing call state, registers, return information, and local data. Unlike the heap, it is usually managed implicitly by the operating system and compiler-generated code. section 7.4.3

A function call creates a stack frame for local variables and saved execution state; returning from the function deallocates that frame. A special stack register tracks the top, and the stack typically grows toward lower virtual addresses. section 7.4.3

Use the stack with these operational expectations:

  • Treat insertion and deletion as constant-time operations because elements are handled at the stack boundary rather than at arbitrary positions. section 7.2
  • Do not choose it for searching: stack search remains linear-time. section 7.2
  • Expect stack memory to grow dynamically as execution uses more of it; the operating system commonly allocates needed memory lazily. section 7.4.3

Function execution

When one function calls another, the stack can preserve registers and other call state, record where execution returns, and provide a frame for the called function's local variables. Returning removes that frame. section 7.4.3

Figure 7.4 A process’s virtual memory address space consists of sections that serve different purposes. The text, data, and BSS sections are initialized at program startup, whereas the heap, memory-mapped region, and stack sections change dynamically at runtime (denoted with dotted lines).

Compare the stack with the heap and other process-memory sections, including which regions change dynamically during runtime. section 7.4.3

How Latency covers it

How deep it goes
Explains stack frames, saved registers, return information, address growth, and lazy allocation, but defers architecture-specific implementation details.
Examples
Uses function calls, local variables, saved registers, return information, and process-memory diagrams as examples.
What you can do after
Evaluate stack suitability by relating function-execution behavior and insertion, deletion, and search complexity to latency-sensitive workloads.
How it connects
Connects stacks with virtual memory, the heap, stack frames, function calls, registers, and algorithmic complexity.

Also explained in

Compare how 14 books explain stack →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.