An example of static and dynamic initialization

Is there an example where a variable is initialized in both a static and dynamic way. That is, as stated in the specification.

Static initialization must be performed before any dynamic initialization is performed.

I am guessing that it is possible that a variable can be initialized in a static and dynamic way in the same program. Although, I think that initialization is defining an initial value. That is, a variable initialized once can no longer be initialized.

+2


source to share


1 answer


Of course: static initialization, filling all objects with zero, is done before any custom code runs.

These zeros can be seen while running other initializers. Read about Fiasco Static Initialization Order

Note that objects are not considered built until dynamic initialization is complete and you must follow the rules of life. [basic.life]

the standard says:

Object lifetime is a property of the object's runtime. An object is said to have non-trivial initialization if it is of a class or aggregate type and it or one of its members is initialized with a constructor other than the trivial default constructor. [Note: initialization with the trivial constructor copy / move is non-trivial initialization. - end note] The lifetime of a type object T

begins when:

  • got a store with the correct alignment and size for the type T

    and
  • If the object has non-trivial initialization, its initialization is complete.

and



Properties assigned to objects within the scope of this International Standard apply to that object only during its lifetime. [Note. In particular, there are significant restrictions on the use of the object before and after the life of an object , as described below in 12.6.2 and 12.7. In addition, the behavior of an object under development and destruction may not be the same as the behavior of an object whose life time began and did not end. 12.6.2 and 12.7 describe the behavior of objects during the construction and destruction stages. - end note]

and



Likewise, before the object's lifetime has begun, but after the storage that the object will occupy has been allocated or, after the object's lifetime has expired and before the storage that the object is occupied, reused, or released, any value glvalue which refers to the original object, but can only be used in a limited way. For the construction or destruction of a facility, see 12.7. Otherwise, such a glvalue refers to the allocated storage (3.7.4.2), and the use of glvalue properties that are independent of its value is well defined. A program has undefined behavior if:

  • an lvalue-to-rvalue conversion (4.1) is applied for such a glvalue,
  • glvalue is used to access a non-static data member or call a non-static member function of an object, or
  • glvalue is bound to a virtual base class reference (8.5.3) or
  • glvalue is used as the operand of a dynamic_cast

    (5.2.7) or as an operand typeid

    .

Thus, accessing the (nullified) content of non-primitive objects can easily lead to undefined behavior.

+4


source







All Articles