ENع
←Stack, heap and globals
Lesson 35 · Registers and the memory map

Living in 2 KB of RAM: stack, heap and globals

12 min10 steps5 questionsIntermediate
About this lesson

An Arduino Uno R3 has just 2,048 bytes of SRAM, and every variable in your sketch has to fit. Follow one pass of a sketch byte by byte: globals fill the bottom, malloc() grows the heap up, function calls push stack frames down from the top, free() leaves a hole, and finally a large local array pushes the stack into the heap without any warning. The C++ lessons “Variables on the stack”, “The heap: new and delete” and “Pointers” cover the language side; here we look at what it costs on a ATmega328P.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 35 · Interactive
01 / 10

2,048 bytes for everything

The Uno's ATmega328P has 2 KB of SRAM: 2,048 bytes at addresses 0x0100 to 0x08FF. Every variable lives here. Three users share it: globals at the bottom, the heap above them, and the stack coming down from the top.

SRAM     2,048 bytes   0x0100 .. 0x08FF
  bottom   globals   (fixed when the sketch is built)
  above    heap      (grows up:   malloc)
  top      stack     (grows down: function calls)
below 0x0100: the chip's registers, not your memory
Loading figure…
or ← →
Read the lesson step by step
  1. 2,048 bytes for everything

    The Uno's ATmega328P has 2 KB of SRAM: 2,048 bytes at addresses 0x0100 to 0x08FF. Every variable lives here. Three users share it: globals at the bottom, the heap above them, and the stack coming down from the top.

    SRAM     2,048 bytes   0x0100 .. 0x08FF
      bottom   globals   (fixed when the sketch is built)
      above    heap      (grows up:   malloc)
      top      stack     (grows down: function calls)
    below 0x0100: the chip's registers, not your memory
  2. Globals take the bottom

    readings is a global: declared outside any function, it lives for the whole run. An int is 2 bytes, so 400 of them take 800 bytes, from 0x0100 to 0x041F. That space is reserved before setup() runs.

    line 1    int readings[400];
    size      400 x 2 bytes = 800 bytes
    where     0x0100 .. 0x041F
    left      1,248 bytes for the heap and the stack
  3. A call pushes a frame

    The stack starts at the top, 0x08FF, and grows down. Calling report() pushes a frame: the 2-byte return address plus its local array line, 200 bytes. The stack pointer, SP, drops at least 202 bytes, to 0x0835.

    line 3    report();
    frame     return address  2 bytes
              char line[200]  200 bytes
              at least 202 bytes: 0x0836 .. 0x08FF
    SP        0x08FF  ->  0x0835
  4. Return pops it

    When report() returns, SP moves back up to 0x08FF and those 202 bytes are free again. A local lives only while its function runs. The stack cleans up after itself, with no code from you.

    report() returns
    SP        0x0835  ->  0x08FF
    line[200] is gone: its bytes are free for the next call
  5. The heap grows up

    malloc(200) asks for memory while the sketch runs. It comes from the heap, which starts right after the globals, at 0x0420. A 2-byte size header goes in front, so a uses 202 bytes. Then b uses 102. The heap top climbs to 0x0550.

    line 4    a = malloc(200)   2 + 200 = 202 bytes   0x0420 .. 0x04E9
    line 5    b = malloc(100)   2 + 100 = 102 bytes   0x04EA .. 0x054F
    heap top  0x0420  ->  0x0550
  6. free() leaves a hole

    free(a) gives its 202 bytes back, but b still sits above them, so the heap cannot shrink. Then malloc(300) needs 302 bytes: too big for the hole, so it goes on top. The heap top climbs to 0x067E. Unusable holes like this are fragmentation.

    line 6    free(a)      hole of 202 bytes at 0x0420; b is above it
    line 7    c = malloc(300)   needs 302 > 202: does not fit the hole
              placed on top  0x0550 .. 0x067D
    heap top  0x0550  ->  0x067E
    in use 404 bytes, but the heap spans 606
  7. The free space between

    Free memory is the gap between the heap top, 0x067E, and the stack at the top: 642 bytes. The heap grows up into it and the stack grows down into it. Nothing in the chip keeps them apart.

    heap top  0x067E
    stack     0x08FF (SP, between calls)
    free      0x0900 - 0x067E = 642 bytes
    no hardware guard between the heap and the stack
  8. malloc() checks first

    Suppose loop() now asked for malloc(700): 702 bytes. avr-libc will not grow the heap within 32 bytes of the stack, so malloc returns NULL, a pointer to nothing. Always check the result before you use it.

    malloc(700)   needs 2 + 700 = 702 bytes
    free 642 bytes, minus the 32-byte margin = 610
    702 > 610  ->  returns NULL
    int *p = (int*)malloc(700);
    if (p == NULL) { /* no memory: cope */ }
  9. The stack never checks

    smooth() declares int window[400]: 800 bytes. Its frame needs at least 802, but only 642 are free. SP drops to 0x05DD and the frame silently overwrites the top 160 bytes of c. No error: values change, and the sketch may crash.

    line 8    smooth();   int window[400]
    frame     2 + 800 = 802 bytes  >  642 free
    SP        0x08FF  ->  0x05DD
    overlap   0x05DE .. 0x067D: 160 bytes of c overwritten
    no check, no error message: anything can happen
  10. Living in 2 KB

    Keep big arrays out of functions, avoid malloc() in loop(), and check what malloc() returns. Wrap fixed text in F(), as in Serial.print(F("Hi")), so it stays in flash instead of being copied into SRAM at start-up.

    big arrays       global, sized to what you need
    malloc() / free() in loop()  ->  holes; avoid
    malloc() result  check for NULL
    fixed text       Serial.print(F("Logging..."));  stays in flash
    recursion        every call adds a frame: keep it shallow

Quiz Unlocks after the last step.