Recursion, Scope and Storage

Article · 2:00

The call stack, and the four storage classes that decide where a variable lives and how long.

A recursive function calls itself, and needs two things: a base case that returns without recursing, and a recursive case that moves toward it. Each call gets its own stack frame holding its parameters and locals, so the depth of the recursion is a real memory cost - roughly a few dozen bytes per frame. Recurse a few hundred thousand deep and you get a stack overflow, which on most systems is a segmentation fault with no explanation. Tail recursion may be optimised into a loop, but the standard does not require it, so do not depend on it.

Scope is where a name is visible: block scope inside braces, file scope outside every function. Lifetime is how long the storage exists, and the two are not the same - which is what the storage classes control.

auto is the default for a local and means "created on entry, destroyed on exit"; nobody writes it. static inside a function keeps the variable alive between calls while keeping its name local - a counter that remembers. static at file scope means the opposite thing: not "keep it alive" but "hide it from other files". That one keyword meaning two different things is a genuine wart worth knowing. extern declares that a variable is defined in another translation unit. register was a hint to keep a value in a CPU register and is ignored by every modern compiler.

The error this phase exists to prevent: returning a pointer to a local variable. The frame is gone the moment the function returns, and the pointer now addresses memory that the next call will reuse.

#include <stdio.h>

int global_counter = 0;        /* file scope, external linkage */
static int file_private = 0;   /* file scope, THIS FILE ONLY */

unsigned long factorial(unsigned n)
{
    if (n <= 1) return 1;              /* base case - always first */
    return n * factorial(n - 1);       /* moves toward it */
}

int next_id(void)
{
    /* static INSIDE a function: lives for the whole program, but only
       this function can see it. Initialised once, not on every call. */
    static int id = 0;
    return ++id;
}

/* THE bug this topic exists for. The frame is gone on return, and the
   pointer now addresses memory the next call will reuse. */
const char *broken(void)
{
    char buffer[32] = "I will not survive";
    return buffer;             /* -Wall warns. Listen to it. */
}

int main(void)
{
    printf("%lu\n", factorial(10));
    printf("%d %d %d\n", next_id(), next_id(), next_id());   /* 1 2 3 */

    {
        int shadow = 1;        /* block scope: gone at the brace */
        printf("%d\n", shadow);
    }
    /* printf("%d\n", shadow);   - not in scope here */

    global_counter++;
    file_private++;
    return 0;
}

/* Each recursive call is a stack frame. A few hundred thousand deep is
   a stack overflow, which arrives as a segmentation fault with no
   message - one reason to prefer a loop where the depth is unbounded. */