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
Storage duration is the manner in which an object has memory allocated for it. Storage duration applies to objects. Scope and storage duration are separate issues.
Automatic storage duration. An automatic variable is declared within a block of code and is recreated every time program execution enters that block. When the block ends, the variable ceases to exist and the memory occupied by it is freed. The C keyword auto defines this storage duration.
In object-oriented programming (OOP), the object lifetime (or life cycle) of an object is the time between an object's creation and its destruction.
The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11), see reference initialization for details.
Storage duration is one of four words:
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).
You walk in to a McDonald's and are presented with a menu. There are many different items on the menu, including a Big Mac, a Quarter Pounder w/Cheese, a hot apple pie, and the ever-disgusting Filet-O-Fish.
You walk up to the counter and order a Big Mac. You are presented with with a 3-layer bun, 2 "all beef" patties and some special sauce, all enclosed in a piece of paper.
The person behind you orders the same thing, and they are given their own burger. You both sit down and start to eat. You eat yours very quickly, but your neighbor barely touches his.
The 3rd person in line orders the Apple Pie, and they are given something entirely different. A little semi-cylindrical pastry filled with something resembling apples.
In this analogy, the printing on the menu is the storage duration, and the type of burger itself is akin to the object lifetime. Two different storage durations were selected; the Big Mac and the Apple Pie. Three objects were produced as a result: two burgers and one pastry. Two of those objects have the same general makeup, even though they are two distinct burgers, but the third is different. Two storage durations, three objects.
Your quote:
Lifetime of an object is equal to or is nested within the lifetime of its storage.
Is not a definition for "lifetime" or "storage duration", but simply relates the two. It tells you, given a "storage duration" of X you can expect a lifetime of Y.
In that sense, the two terms are really twos sides of the same coin. A particular storage duration yields a particular lifetime.
This is elaborated on in the (C++03) Standard:
1/The lifetime of an object is a runtime property of the object. The lifetime of an object of type T begins when: — storage with the proper alignment and size for type T is obtained, and — if T is a class type with a non-trivial constructor (12.1), the constructor call has completed. The lifetime of an object of type T ends when: — if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or — the storage which the object occupies is reused or released.
1/Storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object. The storage duration is determined by the construct used to create the object and is one of the following: — static storage duration — automatic storage duration — dynamic storage duration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With