liveBook · Manning

Stack

As explained in Embedded Software with Rust

A stack is a last-in, first-out memory structure used for nested execution state and temporary data.

A stack holds temporary execution state in last-in, first-out order. In bare-metal Rust, it stores function arguments, local variables, return addresses, and register frames saved on exception entry. Each function’s frame is cleaned up automatically when the function returns. section 7.1.1

The stack occupies a bounded region of RAM. As functions are called, the stack pointer moves down; as they return, it moves back up. Allocation and cleanup are fast, but available RAM is limited. A recursive function can exhaust the stack and silently corrupt static data. section 7.1.1 section 7.0

SECTIONS
{
  .vector_table :
  {
    LONG(ORIGIN(RAM) + LENGTH(RAM));     /* Initial stack pointer (
        top of RAM) */
    KEEP(*(.vector_table.reset_vector))   /* Reset vector (
        entry point) */
    KEEP(*(.vector_table.exceptions))     /* Other exceptions and
    interrupts */
  } > FLASH

This linker-script fragment places the initial stack pointer at the end of RAM as the first Cortex-M vector-table entry. The next entry identifies the reset handler. section 4.1.3

Unlike stack frames, static data persists for the program’s lifetime. The heap supports data with runtime-dependent size or lifetime, but in no_std firmware it requires an explicitly reserved RAM region and allocator. Fixed-capacity containers from heapless or arrayvec offer heap-free alternatives. section 7.1.1 section 7.5

Keep stack usage within the available RAM, especially when using recursion or large local data. Tools and protections include cargo-call-stack, emitted stack sizes, flip-link, software canaries, and MPU guard regions. section 7.0

How Embedded Software with Rust covers it

How deep it goes
Explains frame contents, stack-pointer movement, startup placement, and overflow risks; identifies detection and prevention tools.
Examples
Uses a Cortex-M vector-table linker fragment and a recursive function that can corrupt static data.
What it assumes
Assumes familiarity with linker-defined memory regions and reset-handler initialization of .data and .bss.
What you can do after
Evaluate stack placement and overflow risks when choosing how firmware uses limited RAM.
How it connects
Connects stack frames to static memory, heap allocation, linker scripts, exception handling, and fixed-capacity containers.

Also explained in

Compare how 17 books explain stack →

Related concepts

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