Jonathan Müller

An (In-)Complete Guide to C++ Object Lifetimes

In C++, an object's life doesn't always start when memory is allocated. This guide explores the subtle rules that prevent dangling pointers and undefined behavior.

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.

Featured Partners

Related Articles

View all articles
BR
Benjamin Ruschin
What Developers Really Need to Create Great Code Demos
Every developer on earth has, at some point, had another developer to thank for a breakthrough, a success, an aha moment they wouldn’t have had without coming across that blog post, that open-source contribution, that reply on socials or that humble ...
What Developers Really Need to Create Great Code Demos
CH
Chris Heilmann
All the videos of Halfstack London 2024!
Last month was Halfstack London, a conference about the web, JavaScript and half a dozen other things. We were there to deliver a talk, but also to record all the sessions and we're happy to share them with you. It took a bit as we had to wait for th...
All the videos of Halfstack London 2024!
DC
Daniel Cranney
Dev Digest 195: End of Likes, JavaScript’s a Zoo, and Messing with Bots!
Inside last week’s Dev Digest 195 . 👎 No more external likes 🤗 Needy programs 📉 The worst selling Microsoft product 🟨 JavaScript engines zoo 🍞 No more toasts! 🤖 Messing with bots 👔 Beware of fake job interviews 🗞️ Join over 150,000 developers alread...
Dev Digest 195: End of Likes, JavaScript’s a Zoo, and Messing with Bots!

From learning to earning

Jobs that call for the skills explored in this talk.

Rust and GoLang

Rust and GoLang

NHe4a GmbH
Karlsruhe, Germany

Remote
55-65K
Intermediate
Senior
Go
Rust
Techlead C++

Techlead C++

REDLAB
Paris, France

QT
C++
Software Architecture
Softwareentwickler C++

Softwareentwickler C++

MOBA Mobile Automation AG
Limburg a.d. Lahn, Germany

QT
C++
XML
Linux
Microsoft Office