Stack vs. Heap Allocation¶
Memory with lexically-scoped lifetime can be allocated on the stack (fast, automatic deallocation), while memory with dynamic lifetime requires heap allocation (flexible, but requires explicit or automatic reclamation).
Tension Addressed¶
Convenience vs. Control
Recognizable As¶
Local variables vs. dynamically allocated objects; "stack overflow" errors; function call frames; escaping closures requiring heap allocation; "boxing" to move stack values to heap
Canonical Sources¶
Aho, Sethi & Ullman, Compilers: Principles, Techniques, and Tools (1986); Appel, Modern Compiler Implementation (1998)
Transfers To¶
Resource management patterns generally, scope-based cleanup, any system with nested activation contexts
The stack-heap distinction derives from a fundamental truth: some lifetimes are predictable (tied to lexical scope), others are not (determined by program logic). Stack allocation exploits predictable lifetimes for performance; heap allocation handles everything else. This dichotomy appears in any resource management domain.