In C++, does initializing a global variable with itself have undefined behaviour?

C++Language LawyerUndefined Behavior

C++ Problem Overview


int i = i;

int main() { 
 int a = a;
 return 0;
} 

int a = a surely has undefined behaviour (UB), and more details on it is in https://stackoverflow.com/questions/67663015/is-reading-an-uninitialized-value-always-an-undefined-behaviour-or-are-there-ex.

But what about int i = i? In C++ we are allowed to assign nonconstant values to globals. i is declared and zero initialized (since it has file scope) before the declaration is encountered. In which case we are assigning 0 to it later in the definition. Is it safe to say this does not have UB?

C++ Solutions


Solution 1 - C++

Surprisingly, this is not undefined behavior.

> Static initialization [basic.start.static] > > Constant initialization is performed if a variable or temporary object > with static or thread storage duration is constant-initialized. If > constant initialization is not performed, a variable with static > storage duration or thread storage duration is zero-initialized. > Together, zero-initialization and constant initialization are called > static initialization; all other initialization is dynamic > initialization. All static initialization strongly happens before any > dynamic initialization.

Important parts bold-faced. "Static initialization" includes global variable initialization, "static storage duration" includes global variables, and the above clause is applicable here:

int i = i;

This is not constant-initialization. Therefore, zero-initialization is done according to the above clause (for basic integer types zero-initialization means, unsurprising, that it's set to 0). The above clause also specifies that zero initialization must take place before dynamic initialization.

So, what happens here:

  1. i is initialized to 0.
  2. i is then dynamically initialized, from itself, so it still remains 0.

Solution 2 - C++

The behavior might be undefined for i, since depending on how you read the standard, you could be reading i before its lifetime starts.

> ### [basic.life]/1.2 > > ... The lifetime of an object of type T begins when: > > — its initialization (if any) is complete ...

As mentioned in the other answer, i is initialized twice: first zero-initialized statically, then initialized with i dynamically.

Which initialization starts the lifetime? The first one or the final one?

The standard is being vague, and there are conflicting notes in it (albeit all of them are non-normative). Firstly, there is a footnote in [basic.life]/6 (thanks @eerorika) that explicitly says that the dynamic initialization starts the lifetime:

> ### [basic.life]/6 > > Before the lifetime of an object has started but after the storage which the object will occupy has been allocated26 > > ... > > 26) For example, before the dynamic initialization of an object with static storage duration ...

This interpretation makes the most sense to me, because otherwise it would be legal to access class instances before they undergo dynamic initialization, before they could estabilish their invariants (including the standard library classes defined by the standard).

There's also a conflicting note in [basic.start.static]/3, but that one is older than the one I mentioned above.

Solution 3 - C++

It appears to me int i = i; has undefined behavior, is not caused by the indeterminate value. The term indeterminate value is designed for the objects that have automatic or dynamic storage duration.

[basic.indet#1] > When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced ([expr.ass]).

[basic.indet#2] > If an indeterminate value is produced by an evaluation, the behavior is undefined except in the following cases...

In your example, the object named i has a static storage duration, hence it is not within the extent of talking about indeterminate value. And, such an object has a zero-initialization that happens before any dynamic initialization as per [basic.start.static#2] > Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. All static initialization strongly happens before ([intro.races]) any dynamic initialization.

Hence, its initial value is zero. when i is used as an initializer to initialize itself. which is a dynamic initialization and it obeys [dcl.init].
> Otherwise, the initial value of the object being initialized is the (possibly converted) value of the initializer expression.

It violates the rule in [basic.lifetime]

> The program has undefined behavior if: >> - the glvalue is used to access the object, or

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanView Question on Stackoverflow
Solution 1 - C++Sam VarshavchikView Answer on Stackoverflow
Solution 2 - C++HolyBlackCatView Answer on Stackoverflow
Solution 3 - C++xmh0511View Answer on Stackoverflow