3

In this topic they said that zero initialization is not static initialization.

Can anyone explain why?

3.6.2/2 said:

Together, zero-initialization and constant initialization are called static initialization;

It is definition of Static initialization, means that zero-initialization is static initialization and constant-initialization is static Initialization

Community
  • 1
  • 1
St.Antario
  • 26,175
  • 41
  • 130
  • 318

4 Answers4

1

This answer assumes that you know what static storage duration means.

In C++03 this is specified as (3.6.2):

Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization.

In practice, a program has different memory segments where it stores variables with static storage duration:

  • One segment is usually called .bss, where all static storage variables that are initialized to zero are stored.
  • Another segment is usually called .data, where all static storage variables that are explicitly initialized to a value are stored.
  • And further, there is a segment called .rodata where all const variables are stored.

(The reason why these are two different segments is mainly program startup performance, you can read more about that here.)

Zero initialization applies to all variables stored in .bss and constant initialization applies to all variables stored in .data. (And perhaps constant initialization applies to .rodata as well, depending on whether your system is RAM-based or if it has true ROM).

Collectively, all of this is called static initialization, since it applies to objects with static storage duration.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • As a side note, C works in exactly the same manner, but is less formal with the terms. I would suppose the reason why C++ is more formal is because it has constructors (just speculation). – Lundin Jul 01 '14 at 14:30
  • In C, "All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals." This means that there is no dynamic initialization (which means that the specification can be much simpler). – James Kanze Jul 01 '14 at 14:41
  • Dynamic initialization isn't called _static initialization_. In the case of systems that load the program image from disk, it is _usual_ for data with constant initializers to be be in the `.data` segment; those with dynamic initializers will usually be in the `.bss` segment. And in the compilers I've worked on, `const` objects with constant initialization were in the `.text` segment. – James Kanze Jul 01 '14 at 14:51
  • @JamesKanze I've noticed that `.rodata` and `.text` aren't consistently used across various platforms. `.text` tend to contain the actual executable code, so I would guess it depends on whether the compiler puts the constants together with the code or not. Maybe I'm too used at working with embedded systems where there is always true ROM memory available: in such cases it makes more sense to store constants in their own segment, separated from the code. – Lundin Jul 02 '14 at 06:24
  • @JamesKanze Yes, but often you want the const variables in another physical memory, such as a data flash, which can get easily updated in runtime (user configurations etc). – Lundin Jul 02 '14 at 09:22
0

You forgot to notify the word "Together", very important in this sentence.

Zero-initialization + constant initialization = static initialization. Is that clearer ?

Kabulan0lak
  • 2,116
  • 1
  • 19
  • 34
0

It's just vocabulary. There are clearly three phases of initialization (for variables with static lifetime): zero initialization, initialization using constant expressions, and dynamic initialization. I find it convenient when talking about this to use the term static initialization for the second step (because it does take place statically, without the execution of any user written code), even if the standard uses a somewhat different terminology. In the end, it comes down to the same thing:

int a;
int b = 42;
int c = someFunction();

Formally, all three variables will be zero-initialized. Then b will be initialized with the constant expression 42; in all likelyhood, it will never actually be zero-initialized, because there's no way your code can ever see it before the constant initialization. Finally, c will be initialized by calling someFunction().

This order is true regardless of the order of the definitions, and is guaranteed by the standard.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
-2

The post you link to says that zero-initialization is not static initialization. This is correct.

That is very different from zero-initialization is not a static initialization! This is not correct.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055