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

7
  • That only applies to non-local variables. Zero-initialization can happen in other contexts where it is not static. Jul 1, 2014 at 14:15
  • The reason is simply because the person who posted that answer is being unhelpful, and I've downvoted and commented on that answer for that reason.
    – user743382
    Jul 1, 2014 at 14:16
  • @hvd Maybe you could elaborate, or provide an answer. Jul 1, 2014 at 14:22
  • @juanchopanza Can you provide a Zero Initialization which is not a static initialization. It would be clear...
    – St.Antario
    Jul 1, 2014 at 14:24
  • For example, inside a function, auto i = int();, or any value initializaiton of an aggregate containing built-ins. Jul 1, 2014 at 14:31

4 Answers 4

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.

5
  • 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 1, 2014 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). Jul 1, 2014 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. Jul 1, 2014 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 2, 2014 at 6: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 2, 2014 at 9:22
0

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

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

1
  • Can you provide an example of Static Initialization along with explanation?
    – St.Antario
    Jul 1, 2014 at 14:09
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.

-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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.