1

I had some problems with initialising a boolean array and found this answer which stated that "elements have an initial value of false (that is 0) if declared at file scope and indeterminate if declared at block scope." This solved the issue causing my problem but now I'm wondering, why is that?

Lover of Structure
  • 1,561
  • 3
  • 11
  • 27
Willem
  • 81
  • 9
  • and where is your [mcve]? – OldProgrammer May 25 '23 at 13:54
  • 2
    Because that is what the standard defines. And that is valid for all variable types, not only boolean arrays. Non-static local variables are not initialized by default. If you want them to be initialized, you must do it explicitely. – Gerhardh May 25 '23 at 13:56
  • 2
    @Gerhardh: the question is why the standard made this definition. – Yves Daoust May 25 '23 at 14:09
  • 2
    @OldProgrammer: An MRE is typically needed for a debugging question, not a question about language design. – Eric Postpischil May 25 '23 at 14:10
  • It's not the *scope* that matters, it's the *storage duration*; specifically, whether the object has *automatic* storage duration or not (although objects at file scope cannot have automatic storage duration). – Ian Abbott May 25 '23 at 14:20

4 Answers4

1

Because the standard states so.

From the C standard C11 6.7.9/10:

"... If an object that has static or thread storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;"

Harith
  • 4,663
  • 1
  • 5
  • 20
  • thank you, but does it go deeper than that? I mean, is there a specific reason as to why that is? – Willem May 25 '23 at 13:58
  • @Willem That question has already been asked here: https://stackoverflow.com/q/2091499/20017547 – Harith May 25 '23 at 14:00
  • 2
    @Willem The technical reason is that static variables have a lifetime of the whole program execution and can be initialized once by the compiler and/or by the startup code. Automatic variables would need initialization every time the block is entered. – Bodo May 25 '23 at 14:02
1
  • All variables declared outside of a function or with the storage class-specifier static have static storage duration.
  • If a variable with static storage duration isn't initialized explicitly by the programmer, it is initialized to zero before main() is called. If you initialize a bool to zero it's the same thing as initializing it to false.
  • Whereas all variables declared inside a function or as function parameters (often called "locals") have automatic storage duration. If these aren't initialized explicitly by the programmer, their values are indeterminate, essentially meaning unpredictable garbage values.
Lundin
  • 195,001
  • 40
  • 254
  • 396
1

Variables declared at file scope can be initialized at load time, when the executable image is brought to memory. This has virtually no cost.

But initializing variables at block scope would require a memset or memcpy every time the block is entered, possibly for nothing, and can have a dramatic impact on the cost.

Yves Daoust
  • 672
  • 9
  • 1
    "This has virtually no cost." You are assuming a RAM-based system such as a PC. On ROM-based systems such as microcontrollers, there is an overhead during startup when .bss/.data segments are initialized. And in general there's no such thing as a free lunch. On a PC the RAM is filled up during program start-up by the OS. It has no cost for _your_ program but that's only because the zero-out/copy-down code is executed by the OS instead. The user will have to twiddle their thumbs for the same amount of microseconds either way :) – Lundin May 25 '23 at 14:05
  • @Lundin: in other words, that cost is a part of the total load time, and you don't know the proportions. – Yves Daoust May 25 '23 at 14:07
0

The reason is that objects of 'static' storage duration without an initializer are initialized to zero or the null pointer (as applicable) [applied recursively to: array elements, structure members, the first member of unions].

Arrays declared at file scope have a 'static' storage duration, independent of whether they are declared with the keyword extern, with the keyword static, or without either.

Arrays declared within a function have an 'automatic' storage duration, meaning that they are not initialized and contain garbage values.

As for why this is so, I can only speculate, but the reason is very likely the following:

  • initialization of objects with 'static' storage duration needs to be done only once
  • initialization of objects with 'automatic' storage duration needs to be done whenever the respective function is entered – hence there is a performance cost.

The relevant part of the standard (C17) is (draft, 6.7.9 ¶10):

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:

  • if it has pointer type, it is initialized to a null pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero; [...]
Lover of Structure
  • 1,561
  • 3
  • 11
  • 27