storage duration vs lifetime

c++

Solution

Storage duration is one of four words:

- automatic

- static

- dynamic

- thread (local)

That's it. It tells you what rules apply for when the object will be created and destroyed.

Lifetime is the portion of the runtime of the program during which the object is usable. Generally this is from construction until destruction, but for trivial types (those with no constructor or destructor) it's "from when the memory is allocated until the memory is either released or used for another object".

So the two are related, but they aren't quite the same thing. Two objects with different storage durations could have related and almost-identical lifetimes (for example an automatic `unique_ptr` and the dynamic object it manages), and two objects with the same storage duration can have completely different lifetimes (especially two dynamic objects).

Problem

Can someone explain to me the difference between `storage duration` and `lifetime` of objects? I think they denote the same thing. I found a definition that says: Lifetime of an object is equal to or is nested within the lifetime of its storage. So according to this, there is a little difference I can't see. Also, I'll be very grateful if someone explains to me these concepts in low level terms. I'd rather think about `memory`, `adresses`, and `data` than about high level stuff. Thanks. Link to the definition above

Original source

Related problems