1

Consider

//#include stuff const int x=5; . . int main() { static int var=x; . . } 

If I understand correctly,

  • static variables are zero-initialized whether or not any initialization is indicated, so static int var; alone would set var to zero and static int var=x; also would set var to zero at first.
  • Now static int var=x; does a constant expression initialization which IIRC is done after zero-initialization.

Are static variables initialized multiple times - though the phrase contradicts itself?

9
  • not sure what your question is exactly. You described exactly what happens: x is set to 0 then 5, conceptually. Call it what you will Commented Jul 15, 2016 at 2:32
  • @M.M : I see, thankyou, is there any use of such an implementation? Commented Jul 15, 2016 at 2:38
  • 2
    @songyuanyao see [basic.start.init]/2 "Variables with static storage duration [...] shall be zero-initialized before any other initialization takes place" Commented Jul 15, 2016 at 2:42
  • 1
    @sjsam I don't know the rationale, but it does have the benefit that in static-initialization-fiasco scenarios , you at least read 0 instead of undefined behaviour Commented Jul 15, 2016 at 2:43
  • 1
    In some cases, you can even guarantee a 0 value. Commented Jul 15, 2016 at 3:15

1 Answer 1

5

C++14 states:

3.6.2 Initialization of non-local variables

...

Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.

Ok, this appears to be the foundation for this question.

Constant initialization is performed:

[ ... ]

This is followed by the definition of constant initialization. To make a long story short, both const int x=5; and static int var=x; seem to meet the requirements for constant initialization.

So, based on that, this seems to suggest that "static variables [are] initialized multiple times".

Forging ahead:

Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. Static initialization shall be performed before any dynamic initialization takes place.

So, both zero-initialization and constant initialization, the "multiple" initializations that are the subject matter here, must occur before dynamic initialization.

From this, I conclude that although, technically, the claim that "static variables [are] initialized multiple times" is true, I don't see any way to actually observe it. You need dynamic initialization to observe something. And dynamic initialization doesn't take place until static initialization is completed.

From this, it looks to me like the term "static initialization", as defined in 3.6.2, isn't really the same "static initialization" as in "static initialization fiasco". Seems to me that "static initialization fiasco" should really be called "dynamic initialization fiasco".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.