An (In-)Complete Guide to C++ Object Lifetimes
summary
Covers exact rules that decide whether a C++ object is alive: when definitions, new/placement new, memcpy/memmove/malloc or mmap implicitly or explicitly start lifetimes, and when destructors or storage reuse end them. Explains temporary lifetime extension gotchas, when to use std::launder versus std::bit_cast, and how std::start_lifetime_as lets you treat raw bytes as objects. Shows compiler optimizations (provenance, strict aliasing) that make naive malloc/mmap, union punning, or pointer arithmetic undefined and how these cause surprising concurrency and optimization traps; gives concrete fixes and language-version notes (C++26 change).
AI-written summary · about
chapters (12)
- 0:00C++ object lifetimes intro
- 1:15Objects and lifetimes
- 9:16Variable declarations
- 15:20Dynamic allocation
- 16:46Temporary objects
- 24:30Placement new
- 36:36Implicit object creation C++20
- 41:11Start lifetime explicit
- 48:24Pointer semantics
- 58:25Type punning rules
- 1:07:10Zombie pointers
- 1:18:34Summary guidelines
related talks
58mAn (In-)Complete Guide to C++ Object Lifetimes
49mC++: Objects, their lifetime and pointers
04mLightning Talk: Now in Our Lifetimes Cpp
06mAre We There Yet?
59mA Tour of C++ Recognised User Type Categories
01h 01mSurprises in Object Lifetime
01h 11mDynamic Memory Allocation Challenges in C++ Safety Critical Systems
01h 08mImplementing the C++ Core Guidelines’ Lifetime Safety Profile in Clang
