Why are the elements of a boolean array initialized as false if declared at file scope but undefined if declared at block scope?

huangapple go评论73阅读模式
英文:

Why are the elements of a boolean array initialized as false if declared at file scope but undefined if declared at block scope?

问题

我遇到了一些初始化布尔数组的问题,并发现了这个答案,其中提到"如果在文件范围内声明,元素的初始值为false(即0),如果在块范围内声明,则为不确定。" 这解决了导致我的问题的原因,但现在我想知道,为什么会这样?

英文:

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?

答案1

得分: 1

因为标准规定如此。

来自C标准C11 6.7.9/10:

"... 如果具有静态或线程存储期的对象没有明确初始化,则:

— 如果它具有指针类型,则初始化为null指针;

— 如果它具有算术类型,则初始化为(正数或无符号)零;"

英文:

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

答案2

得分: 1

  • 所有在函数外声明的变量或使用存储类说明符 static 的变量具有 静态存储期
  • 如果具有静态存储期的变量没有被程序员明确初始化,在调用 main() 函数之前它们会被初始化为零。如果你将一个 bool 初始化为零,它与初始化为 false 是相同的。
  • 而所有在函数内部声明或作为函数参数声明的变量(通常称为“局部变量”)具有 自动存储期。如果程序员没有明确初始化它们,它们的值将是 不确定的,基本上意味着不可预测的垃圾值。
英文:
  • 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.

答案3

得分: 1

以下是您要翻译的内容:

在文件范围声明的变量可以在加载时初始化,当可执行映像加载到内存时。这几乎没有成本。

但在块范围初始化变量需要在每次进入块时进行memset或memcpy,可能是多余的,并且可能对成本产生重大影响。

英文:

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.

答案4

得分: 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;
    [...]
英文:

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

huangapple
  • 本文由 发表于 2023年5月25日 21:52:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定