Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Stack and heap

Стек и куча

The call stack tracks active function calls and where execution should return. The heap supplies memory on demand for data that can outlive the function that created it.

Two jobs for program memory

When one function calls another, the program needs to remember where to resume. The call stack holds that bookkeeping. If main calls load and load calls parse, parse normally finishes first. Each return takes execution back to the caller: last in, first out.

The heap serves a different purpose. It lets a program request memory while it runs, such as when it learns the size of an incoming file or creates data that another part of the program will use later. Heap allocations don't have to be released in reverse order. The name has nothing to do with the heap data structure used to implement priority queues.

How allocation and cleanup work

A function call typically gets a stack frame. Depending on the platform and generated code, the frame can hold saved registers, return information, and some local values. When the call returns, its stack space can be reused. Threads usually have separate call stacks, even when they share access to the same heap objects.

Heap allocation goes through an allocator. In C, code pairs a successful malloc with free when the block is no longer needed. In a garbage-collected runtime, the collector can reclaim an object once it's no longer reachable, though it may do so later. Returning from a function doesn't automatically reclaim an object it allocated on the heap.

Example: returning a value from C

#include <stdlib.h>

int *make_score(void) {
    int score = 42;
    int *result = malloc(sizeof *result);
    if (result != NULL) {
        *result = score;
    }
    return result;
}

The function returns a copy of the pointer, not the local pointer variable itself. If allocation succeeds, the caller can read 42 from the allocated block and must eventually free it. Returning &score would be a different story: score stops existing when the function returns, so the caller can't safely read through its address. When the caller only needs the number, returning an int directly is simpler and needs no allocation.

Common interview traps

Don't assume that every local variable occupies a stack slot or that every object must be allocated on the heap. A compiler can keep a local value in a register or eliminate it. Some compilers use escape analysis to determine whether a reference can be used beyond a call, which can help them avoid a heap allocation. Explain the lifetime rules first, then qualify your answer about physical placement by language and implementation.

Also distinguish allocation cost from access cost. Reserving space in a stack frame is often cheap, but that doesn't make every stack read faster than every heap read. Access patterns and whether the data is already in the CPU cache matter.

A useful debugging question is whether the problem comes from too many active calls or too much allocated data. Deep recursion can exhaust a thread's stack even if the program has ample heap space. A cache that keeps growing, by contrast, can exhaust heap memory even when the program rarely calls more than a few functions deep.