在c中未初始化的全局变量总是默认为0吗?

发布于 2024-11-01 10:37:17 字数 179 浏览 2 评论 0原文

我知道未初始化的全局变量会在 BSS 段中恢复,操作系统应该将其初始化为零。

但这是应该而不是必须,而且我从未见过任何标准说未初始化的全局变量必须默认为零,所以它是安全的认为这是理所当然的吗?

I know that uninitialized globals are restored in the BSS segment and the OS should initialize it to zeros.

But it's should not must,and I've never seen any standard saying that uninitialized globals must be default to zeros, so is it safe to take this as granted?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么敢 2024-11-08 10:37:17

这是来自 C99 标准文档的权威答案第 6.7.8 条(第 10 段):

如果具有自动存储持续时间的对象未显式初始化,则其值为
不定。如果具有静态存储持续时间的对象未显式初始化,
then:

  • 如果是指针类型,则初始化为空指针;
  • 如果它是算术类型,则将其初始化为(正或无符号)零;
  • 如果是聚合,则根据这些规则(递归地)初始化每个成员;
  • 如果它是一个联合体,第一个命名的成员将根据这些进行初始化(递归地)
    规则。

Here's the autoritative answer from the C99 Standard Document clause 6.7.8 (paragraph 10):

If an object that has automatic storage duration is not initialized explicitly, its value is
indeterminate. If an object that has static 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;
  • if it is an aggregate, every member is initialized (recursively) according to these rules;
  • if it is a union, the first named member is initialized (recursively) according to these
    rules.
安人多梦 2024-11-08 10:37:17

C 标准规定,具有静态存储持续时间且没有显式初始化的变量(包括所有全局变量)将初始化为零。

其他语言(例如 Fortran)有所不同。

The C standard says that variables with static storage duration (which includes all global variables) without explicit initialization are initialized to zero.

Other languages, such as Fortran, differ.

七堇年 2024-11-08 10:37:17

IIRC 当然没有要求将未初始化的全局变量设置为零,而且我确信我听说过并非如此的情况。与往常一样,请谨慎行事,如果您担心出现问题,请务必自行初始化变量。

我个人尽量绝不认为任何事情都是理所当然的。明确地明确它不仅可以巧妙地避免任何此类麻烦,而且还可以让其他阅读您代码的人清楚地了解您期望的情况。

编辑:我已更正,标准确实要求全局变量初始化为零。只是为了澄清我上面糟糕的措辞,我并不是说绝对没有任何事情可以被认为是理所当然的(这是荒谬的),而是如果有一种简单而简洁的方法认为某事是理所当然的当然,做吧。

我提倡这种方法的原因是,虽然大多数程序员可以依赖具有符合标准行为的编译器,但我们中有很多人在无论出于何种原因并不总是能够遵守标准的环境中工作(微控制器的硬件限制就是一个很好的例子) ,或参阅评论中史蒂夫的示例)。我还认为不存在任何完全符合标准的编译器(除了编译器定义标准的情况)。

当我在文件中看到 int myGlobal=0; 时,我确信 myGlobal 的值为零。如果它只是声明为 int myGlobal;,标准规定它的值也应该为零。这并不能保证它,而且我相信,输入额外的两个字符并没有太大的成本,可以增加程序的可读性,并且如果您发现确实需要编译代码,则可以增加可移植性一个不预先初始化全局变量的平台。 就是我的观点 - 为什么不呢,即使标准说你应该没问题,你也可能只是遮盖自己。

IIRC there's certainly no requirement that uninitialised globals be set to zero, and I'm sure I've heard of cases where that's not the case. As always, play it safe and always initialise your variables yourself if you're afraid of it being an issue.

I personally try to never take anything for granted. Not only does making it explicit neatly circumvent any such troubles, but it also makes it clear for anyone else reading your code what you are expecting to be the case.

EDIT: I've since been corrected that the standards do require globals to be initialised to zero. Just to clarify my poor wording above, I don't mean that absolutely nothing can ever be taken for granted (that's absurd), but rather that if there is a simple and concise way of not taking something for granted, do it.

The reason I advocate this approach is because although most programmers can rely on compilers with standards-compliant behaviour, there are plenty of us that work in environments where standards compliance is not always possible for whatever reason (the hardware limitations of microcontrollers being a good example, or see Steve's example in the comments). I'd also argue that there's not in existence any compiler that is fully standards compliant (other than in those cases where a compiler defines the standard).

When I see int myGlobal=0; in a file, I know for sure that myGlobal has a value of zero. If it is just declared as int myGlobal;, the standard says that is also should have a value of zero. That does not guarantee that it will, and I believe that typing the extra two characters is no big cost, increases readability of the program, and increases portability if you find you ever do need to compile the code on a platform that doesn't pre-initialise globals. That is my point - why not, and you might just cover yourself even if the standard says you should be ok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文