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?

4
  • and where is your minimal reproducible example? Commented May 25, 2023 at 13:54
  • 3
    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. Commented May 25, 2023 at 13:56
  • 2
    @OldProgrammer: An MRE is typically needed for a debugging question, not a question about language design. Commented May 25, 2023 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). Commented May 25, 2023 at 14:20

4 Answers 4

2

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;"

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

2 Comments

@Willem That question has already been asked here: stackoverflow.com/q/2091499/20017547
@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.
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.

Comments

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.

2 Comments

"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: in other words, that cost is a part of the total load time, and you don't know the proportions.
1

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; [...]

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.