未初始化变量的默认值 [英] Default value to non initialized variables

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

问题描述

我正在阅读本教程关于调试.我将阶乘代码粘贴到我的 .c 存档中:

I'm reading this tutorial about debugging. I pasted the factorial code in my .c archive:

#include <stdio.h>

int main()
{
    int i, num, j;
    printf ("Enter the number: ");
    scanf ("%d", &num );

    for (i=1; i<num; i++)
        j=j*i;    

    printf("The factorial of %d is %d\n",num,j);
}

当我运行可执行文件时,它总是打印 0,但是,教程的作者说它返回数字垃圾值.我在谷歌上搜索过这个,我读到这是正确的,除了静态变量.所以它应该返回一个垃圾号而不是 0.

When I run the executable, it always print 0, however, the author of the tutorial says that it return numbers garbage value. I've googled about this and I've read that this is right, except for static variables. So it should return a garbage number instead of 0.

我认为这可能是由于 C 的版本不同,但该指南是 2010 年的.

I thought that this might be due to a different version of C, but the guide is from 2010.

为什么我总是看到 0,而不是垃圾值?

Why do I always see 0, instead of a garbage value?

推荐答案

C99 标准草案C11 草案标准 说未初始化的自动变量的值是不确定的,来自 C99 标准草案部分 6.2.4 Storage durations of objects 段落 5 说(强调我的):

Both the C99 draft standard and the C11 draft standard say the value of an uninitialized automatic variable is indeterminate, from the draft c99 standard section 6.2.4 Storage durations of objects paragraph 5 says (emphasis mine):

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

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 any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new 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.

标准草案将不确定定义为:

the draft standard defines indeterminate as:

未指定的值或陷阱表示

并且一个未指定的值被定义为:

and an unspecified value is defined as:

本国际标准规定的相关类型的有效值没有在任何情况下选择哪个值的要求

valid value of the relevant type where this International Standard imposes no requirements on which value is chosen in any instance

所以值可以是任何东西.它可能因编译器、优化设置而异,甚至可能因运行而异,但不能可靠,因此任何使用不确定值的程序都会调用 未定义行为.

so the value can be anything. It can vary with the compiler, optimization settings and it can even vary from run to run but it can not be relied and thus any program that uses a indeterminate value is invoking undefined behavior.

标准说这在 6.5.2.5 复合文字17 中的一个例子中是未定义的,它说:

The standard says this is undefined in one of the examples in section 6.5.2.5 Compound literals paragraph 17 which says:

请注意,如果使用迭代语句而不是显式的 goto 和带标签的语句,则未命名对象的生命周期将仅是循环体,并且在下次进入 p 时将具有不确定的值,这会导致未定义的行为.

Note that if an iteration statement were used instead of an explicit goto and a labeled statement, the lifetime of the unnamed object would be the body of the loop only, and on entry next time around p would have an indeterminate value, which would result in undefined behavior.

这也包含在 Annex J.2 未定义的行为:

具有自动存储期的对象的值被使用时不确定 (6.2.4, 6.7.8, 6.8).

The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.8, 6.8).

在某些非常具体的情况下,您可以对此类行为做出一些预测,演示文稿 Deep C 进入其中一些.这些类型的检查只能用作进一步了解系统工作方式的工具,甚至不应接近生产系统.

In some very specific cases you can make some predictions about such behavior, the presentation Deep C goes into some of them. These types of examination should only be used as a tool to further understand how systems work and should never even come close to a production system.

这篇关于未初始化变量的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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