Heaps Don't Lie: Guidelines for Memory Allocation in C++
summary
About ten pragmatic rules: prefer stack allocation when size is known because it’s one register add; treat malloc as “fast except when it isn’t” because buckets, syscalls and swap can make it 10×–1000× slower; use mimalloc or another third‑party heap on Windows to avoid CRT lock contention; reserve final container sizes but don’t call reserve with a constant delta in a loop; reuse buffers and prefer output/reference parameters to avoid repeated allocations; avoid alloca for large or threaded work; favor explicit clear over assign‑to‑empty; and consider a monotonic allocator for scoped, budgeted resources.
AI-written summary · about
chapters (16)
- 0:00Introduction and setup
- 1:58About the speaker
- 3:02Why heap allocation matters
- 8:17Stack vs heap allocation
- 15:03Heap allocation unpredictability
- 18:58Third-party heap implementations
- 24:40Reserve container size wisely
- 26:49Avoid reserve in loops
- 29:01Output parameters over containers
- 31:50Reuse allocations to save memory
- 36:43Clear over assign to empty
- 37:25Custom memory allocators overview
- 41:40Monotonic allocators explained
- 46:19When to use monotonic allocators
- 48:04Summary and final advice
- 48:50Q&A session
