IT lexicon › Programming › Stack allocator

Stack allocator

Programming På svenska → Updated: 2026-10-26

An allocator that hands out memory by moving a pointer forward and frees by moving a saved marker back — strictly last-in, first-out.

Instead of asking the OS for every block, a stack allocator reserves one contiguous buffer and "allocates" by pushing a top pointer forward. To return memory you save the marker's position beforehand and roll the pointer back to it — everything allocated after that is freed at once. It is O(1), with no fragmentation at all.

The price is discipline: you must free in the reverse order you allocated, exactly like a call stack. That makes the pattern perfect for temporary scratch memory within a frame and unsuitable for objects with unpredictable lifetimes. The related arena allocator instead frees everything in one go; a pool allocator hands out fixed-size blocks in any order.

← Back to the lexicon