Storage time until service life

Can someone explain to me the difference between storage duration and lifetime objects? I think they mean the same thing. I found a definition that says:

The service life of an object is equal to or invested during its storage period.

So, according to this, there is a slight difference that I do not see. In addition, I will be very grateful if someone will explain these concepts to me at a low level. I'd rather think about memory , adresses and data than high-level materials. Thanks.

Link to the definition above

+6
source share
5 answers

Duration of storage is one of four words:

  • auto
  • static
  • dynamic
  • stream (local)

What is it. It tells you what rules apply when an object is created and destroyed.

Lifetime is part of the program’s runtime during which the object can be used. As a rule, this is from construction to destruction, but for trivial types (those that do not have a constructor or destructor) it is "from the moment memory is allocated until the memory is released or used for another object."

So these two are connected, but they are not quite the same. Two objects with different storage durations can have associated and almost identical lifetimes (for example, automatic unique_ptr and the dynamic object that it manages), and two objects with the same storage duration can have completely different lifetimes (especially two dynamic objects).

+10
source

You go to McDonald and get the menu. There are many different items on the menu, including Big Mac, Quarter Pounder w / Cheese, hot apple pie and the ever-disgusting Fisht-O-Fish.

You go to the rack and order a Big Mac. You are offered a 3-layer bun, 2 "all beef" pies and some special sauce, all embedded in a piece of paper.

The person behind you orders the same thing and they are given their hamburger. You both sit down and start eating. You eat very fast, but your neighbor barely touches it.

The 3rd person in line orders an Apple Pie, and they are given something completely different. A small semi-cylindrical cookie filled with something resembling apples.

In this analogy, menu printing is the duration of storage, and the type of hamburger itself is similar to the lifetime of an object. Two storage durations were selected; Big Mac and Apple Pie. As a result, three objects were created: two burgers and one cookie. Two of these objects have the same general make-up, although they are two different hamburgers, but the third is different. Two storage durations, three objects.


Your quote:

The lifetime of an object is equal to or nested during the life of its storage.

It is not a definition for “lifetime” or “storage duration,” but it simply binds the two together. He tells you that given the “storage time” of X, you can expect a life of Y.

In this sense, these two members are indeed two sides of the same coin. A specific storage time gives a specific service life.

This is described in detail in the standard (C ++ 03):

3.8 Object lifetime

1 / The lifetime of an object is a property of the runtime of an object. The lifetime of an object of type T begins when: - storage with proper alignment and size for type T, and - if T is a class type with a nontrivial 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 nontrivial destructor (12.4), a call to the destructor begins, or - the storage that the object occupies, reuses, or is freed.

3.7 Duration of storage

1 / Duration of storage is a property of an object that determines the minimum potential lifetime of the storage containing the object. Storage duration is determined by the design used to create the object, and is one of the following: - static storage duration - automatic storage duration - dynamic storage duration

+4
source

Simply put, storage duration is the period during which the memory underlying the object will be available; lifetime is the period during which you can use it as an object of the specified type. The most obvious example of the difference is when you call std::vector<>::reserve ; this allocates memory for the corresponding number of objects, but they do not become available until you name some function, them.

In other contexts, trivially constructed objects are considered so that the service life is equal to the duration of their storage, but this is actually not so; until the object is initialized, there is only a limited number of things you can do with it (there is no lvalue to rvalue conversion, in particular). On the other end, the lifetime of objects with non-trivial constructors is considered to be that destructors start when you return from the constructor and end when you enter the destructor, but there are quite a few things that you can do with it while the constructor and destructor . And, of course, for how long since all you do is copy the memory address, the only one that counts as the storage duration.

+2
source

I would think of memory , adresses and data than high-level materials.

Where the data is stored depends on several options specified in

  • automatic => in the function call stack (also see RAII )
  • static => in the (correctly initialized) section of global data (see also RAII )
  • dynamic => on the heap (allocation from new() or malloc() calls)
  • thread (local) => for specific personal data streams ( Note: Attempting to share personal data streams directly between streams results in an exception.) li>

Lifetime , and Duration of storage ; - instances of your class are defined by scope (s), they are separated internally.
Of course, for threads, you could share common areas and should observe this carefully.
Usually check for unwanted copies, i.e. rvalue links.

The preferred paradigm for use with RAII , which is very well supported by the memory management functions of the latest smart pointers .
The latter encapsulate the management of the locked area { ... } and the common "virtual" area, which controls the lifetime of the encapsulated class instances for even dynamically distributed objects.

+1
source

This is what I understand so far (and think it is correct):

The only situation where the lifetime may differ from the storage duration is the use of dynamic objects. For example, two static or two automatic objects have the same lifetime. We can have an automatic object and a dynamic object (different storage durations), and those two can have the same lifetime, if, for example, we end the lifetime of a dynamic object at the end of the function area. This is because we can put an end to the life of a dynamic object when we want, and get the same lifetime as some other object with different storage durations. In addition, we can have two different dynamic objects and give a shorter service life to the first object than the second, freeing up the memory that the first object occupies.

At the end of the day, these definitions are saved:

Lifetime object is started when the constructor is called and continues until the destructor is called. (or from its creation to its removal)

Storage duration in simple words “guarantees” that the object that will be created will be useful (it can use the allocated memory) until the moment indicated by it. For example, if it is a static object, it will act until the program terminates, automatic when the function finishes, and dynamic one when the user calls delete .

Correct me, please, if I'm wrong somewhere.

0
source

All Articles