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

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

问题描述

我在读Nginx的源$ C ​​$ c和发现它不是初始化许多变量的数值,其中包括 ngx_int_t ngx_last_process; ,这里 ngx_int_t 定义为长整型

  0#如果
    ngx_last_process = 0;
#万一

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

但在PROGRAME它假设默认值为 0

 为(S = 0; S< ngx_last_process; S ++){
        如果(ngx_processes [S] .pid == -1){
            打破;
        }
    }

是不是存在保证了未初始化的变量将具有 0 值在C呢?


解决方案

外部的和的静态的变量默认初始化为零,这是保证。的自动的和的注册的没有连接显式的初始化变量会产生不确定的值(未指定的值或陷阱重新presentation)。

从标准:

C89

6.5.7:


  

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


C99

6.2.4,§5:


  

有关这样的对象不具有可变长度数组类型,其寿命延长
  从进入与它相关联,直到该块的执行结束块中的
  无论如何,(进入封闭块或调用函数暂停,但并没有结束,
  当前块的执行。)如果递归输入的块,重新实例
  对象被创建各一次。对象的初始值是不确定的。如果
  初始化为对象指定,这是每次声明进行
  在该块的执行达成;否则,该值变得不确定每个
  报关时间到达。


6.7.8,§10:


  

如果具有自动存储时间的对象没有明确初始化,它的价值是不确定的。如果具有静态存储持续时间的对象不明确初始化,然后:


  
  

    

- 如果它有指针的类型,它被初始化为空指针;


    
    

- 如果它具有算术类型,它被初始化为(正orunsigned)​​为零;


    
    

- 如果它是集料,每件被初始化(递归地)根据这些规则;


    
    

- 如果它是联合,第一名为构件被初始化(递归地)根据这些规则


  

3.17.2,§1:

不确定的值:要么未指定的值或陷阱重新presentation

3.17.3,§1:

未经指定的值:相关类型,其中本国际标准中并没有规定在其上选择在任何情况下的价值要求的有效价值。注:未指定的值不能再presentation一个陷阱。

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

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

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

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

解决方案

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).

From The Standard:

C89

6.5.7:

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.

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

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