Jonathan Müller
An (In-)Complete Guide to C++ Object Lifetimes
#1about 3 minutes
Defining objects and lifetimes in C++
An object is a region of storage with a type and value, and its lifetime is a property on the abstract machine that dictates when it can be safely manipulated.
#2about 5 minutes
Creating objects with variable declarations and storage duration
Objects created by variable definitions have their lifetime influenced by their storage duration (automatic, static, or thread), which is distinct from the object's actual lifetime that begins after initialization.
#3about 5 minutes
Understanding temporary objects and lifetime extension rules
Temporary objects are created for prvalues and are usually destroyed at the end of an expression, but their lifetime can be extended when bound directly to a reference or used in a range-based for loop.
#4about 5 minutes
Manually creating objects in existing memory with placement new
Placement `new` allows constructing an object in pre-allocated storage, and if the new object is "transparently replaceable," existing pointers and references to the old object remain valid.
#5about 2 minutes
Using std::launder for non-transparent object replacement
When an object replacement is not transparent (e.g., for `const` objects), `std::launder` must be used on old pointers to prevent undefined behavior by forcing the compiler to acknowledge the new object in that storage.
#6about 5 minutes
Implicit object creation and std::start_lifetime_as
Certain operations like `malloc` can implicitly create objects to make C-style code valid, and `std::start_lifetime_as` should be used to correctly begin the lifetime of an object in a raw byte buffer.
#7about 2 minutes
Implicit destruction and best practices for object lifetime
Reusing storage with placement `new` implicitly destroys the previous object, and best practices include explicit object creation and using the newly returned pointer to avoid aliasing issues.
Related jobs
Jobs that call for the skills explored in this talk.
Wilken GmbH
Ulm, Germany
Senior
Kubernetes
AI Frameworks
+3
MARKT-PILOT GmbH
Stuttgart, Germany
Remote
€75-90K
Senior
Java
TypeScript
+1
Matching moments
06:28 MIN
Using AI agents to modernize legacy COBOL systems
Devs vs. Marketers, COBOL and Copilot, Make Live Coding Easy and more - The Best of LIVE 2025 - Part 3
02:44 MIN
Rapid-fire thoughts on the future of work
What 2025 Taught Us: A Year-End Special with Hung Lee
05:55 MIN
The security risks of AI-generated code and slopsquatting
Slopquatting, API Keys, Fun with Fonts, Recruiters vs AI and more - The Best of LIVE 2025 - Part 2
03:05 MIN
Building collaborative design tools and the neo-brutalism trend
Slopquatting, API Keys, Fun with Fonts, Recruiters vs AI and more - The Best of LIVE 2025 - Part 2
11:32 MIN
The industry's focus on frameworks over web fundamentals
WeAreDevelopers LIVE – Frontend Inspirations, Web Standards and more
14:14 MIN
Scripting presentations and demos in VS Code
Devs vs. Marketers, COBOL and Copilot, Make Live Coding Easy and more - The Best of LIVE 2025 - Part 3
03:38 MIN
Balancing the trade-off between efficiency and resilience
What 2025 Taught Us: A Year-End Special with Hung Lee
04:57 MIN
Increasing the value of talk recordings post-event
Cat Herding with Lions and Tigers - Christian Heilmann
Featured Partners
Related Videos
The C++ rvalue lifetime disaster
Arno Schoedl
C++ Features You Might Not Know
Jonathan Müller
101 brilliant things of C++
Andreas Fertig
The Design and Evolution of C++
Bjarne Stroustrup
C++ in constrained environments
Bjarne Stroustrup
Guided Memory Management: Rust's Ownership Model
Stefan Baumgartner
Trash Talk - Exploring the memory management in the JVM
Gerrit Grunwald
Functional Programming in C++
Jonathan Müller
Related Articles
View all articles



From learning to earning
Jobs that call for the skills explored in this talk.

Vesterling Consulting GmbH
Berlin, Germany
Remote
€55-80K
API
C++
Bash
+7

Hetzner Online GmbH
Unterföhring, Germany
Senior
C
Go
API
C++
Ceph
+2




KA RESOURCES
Dresden, Germany
€65-70K
Intermediate
QT
C++
Software Architecture
Continuous Integration


