未初始化的整数在c中总是默认为0吗? [英] Is un-initialized integer always default to 0 in c?

查看:42
本文介绍了未初始化的整数在c中总是默认为0吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读nginx的源代码,发现它没有初始化很多数值变量,包括ngx_int_t ngx_last_process;,这里ngx_int_t定义为long int

I'm reading source code of nginx and find it's not initializing many of the numerical variables, including ngx_int_t ngx_last_process;,here ngx_int_t defined as long int

#if 0
    ngx_last_process = 0;
#endif

所以这里@Igor Sysoev 认为没有必要进行初始化?

So here @Igor Sysoev think it unnecessary to do the initialization?

但在程序中它假设默认值为 0:

But in the programe it's assuming the default value is 0:

    for (s = 0; s < ngx_last_process; s++) {
        if (ngx_processes[s].pid == -1) {
            break;
        }
    }

是否保证未初始化的变量在c中有0值?

Is it guranteed that un-initialized variable will have 0 value in c at all?

推荐答案

Externalstatic 变量默认初始化为零,这是有保证的.Automaticregister 没有显式初始化器的变量将具有不确定的值(未指定的值或陷阱表示).

External and static variables are initialized to zero by default, this is guaranteed. Automatic and register variables that do not have en explicit initializer will have an indeterminate value (either an unspecified value or a trap representation).

来自标准:

C89

6.5.7:

如果具有静态存储持续时间的对象未初始化显式地,它被隐式初始化,就好像每个具有算术类型被分配 0 并且每个具有指针类型的成员被分配了一个空指针常量.如果一个对象有自动存储持续时间未显式初始化,其值为不确定.

If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant. If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

C99

6.2.4,§5:

对于这样一个没有变长数组类型的对象,它的生命周期会延长从进入与其关联的块直到该块的执行结束无论如何.(进入封闭块或调用函数会暂停,但不会结束,当前块的执行.)如果块是递归进入的,一个新的实例每次都会创建对象.对象的初始值是不确定的.如果为对象指定初始化,每次声明时执行在执行块时达到;否则,每个值都变得不确定到达声明的时间.

For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in anyway.(Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively,anew instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.

6.7.8,§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 orunsigned) 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.

3.17.2,§1:

不确定值:未指定的值或陷阱表示

indeterminate value: either an unspecified value or a trap representation

3.17.3,§1:

未指定值:相关类型的有效值,其中本国际标准对在任何情况下选择哪个值没有要求.注意 未指定的值不能是陷阱表示.

unspecified value: valid value of the relevant type where this International Standard imposes no requirements on which value is chosen in any instance. NOTE An unspecified value cannot be a trap representation.

这篇关于未初始化的整数在c中总是默认为0吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆