Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storage duration vs lifetime

Tags:

c++

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

like image 646
LearningMath Avatar asked Jan 27 '14 21:01

LearningMath


People also ask

What is duration of storage?

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.

What is storage duration C?

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.

What is the lifetime of an object?

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.

How can you extend the lifetime of an object?

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.


2 Answers

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).

like image 66
Steve Jessop Avatar answered Sep 21 '22 17:09

Steve Jessop


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:

3.8 Object Lifetime

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.

3.7 Storage Duration

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

like image 29
John Dibling Avatar answered Sep 19 '22 17:09

John Dibling