简体   繁体   English

非初始化变量的默认值

[英]Default value to non initialized variables

I'm reading this tutorial about debugging. 我正在阅读有关调试的本教程 I pasted the factorial code in my .c archive: 我在我的.c档案中粘贴了因子代码:

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

When I run the executable, it always print 0 , however, the author of the tutorial says that it return numbers garbage value. 当我运行可执行文件时,它总是打印0 ,但是,教程的作者说它返回数字垃圾值。 I've googled about this and I've read that this is right, except for static variables. 我已经用Google搜索了这个,我已经读过这是正确的,除了静态变量。 So it should return a garbage number instead of 0 . 所以它应该返回一个垃圾数而不是0

I thought that this might be due to a different version of C, but the guide is from 2010. 我认为这可能是由于不同版本的C,但指南是从2010年开始。

Why do I always see 0 , instead of a garbage value? 为什么我总是看到0而不是垃圾值?

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 ): C99标准草案C11标准草案都说未初始化的自动变量的值是不确定的,来自草案c99标准第6.2.45的对象的存储持续时间表示( 强调我的 ):

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. 对于没有可变长度数组类型的此类对象,其生命周期从entry进入与其关联的块,直到该块的执行以任何方式结束。 (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: 标准草案将不确定定义为:

either an unspecified value or a trap representation 要么是未指定的值,要么是陷阱表示

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 . 它可能随编译器,优化设置而变化,甚至可能因运行而异,但不能依赖它,因此任何使用不确定值的程序都会调用未定义的行为

The standard says this is undefined in one of the examples in section 6.5.2.5 Compound literals paragraph 17 which says: 标准说这在6.5.2.5 复合文字17段中的一个例子中未定义,其中说:

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

this is also covered in Annex J.2 Undefined behavior : 这也包含在Annex J.2 未定义的行为

The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.8, 6.8). 具有自动存储持续时间的对象的值在不确定时使用(6.2.4,6.7.8,6.8)。

In some very specific cases you can make some predictions about such behavior, the presentation Deep C goes into some of them. 在某些非常具体的情况下,您可以对此类行为做出一些预测, Deep C将介绍其中的一些行为。 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. 这些类型的检查应仅用作进一步了解系统如何工作的工具,甚至不应该接近生产系统。

You need to initialize j to 1. If j happens to be zero, the answer will always be zero (one type of garbage). 您需要将j初始化为1.如果j恰好为零,则答案将始终为零(一种类型的垃圾)。 If j happens to non-zero, you'll get different garbage. 如果j恰好非零,你会得到不同的垃圾。 Using uninitialized variables is undefined behaviour; 使用未初始化的变量是未定义的行为; 'undefined' does not exclude always being zero in the tests you've done so far. 'undefined'并不排除在目前为止所做的测试中始终为零。

有些系统将其内存设置为0(例如Mac OS),因此当您初始化它时,您的变量通常会包含0,但这会导致不稳定的结果。

You can't say what should happen in this case because the language specification doesn't say what should happen. 你不能说在这种情况下会发生什么,因为语言规范没有说明会发生什么。 In fact it says that the values of uninitialised non-static variables are indeterminate. 事实上,它表示未初始化的非静态变量的值是不确定的。

That means they can be any value. 这意味着它们可以是任何价值。 They can be different values on different runs of your program, or when your code is compiled on a different compiler, or when compiled on the same compiler with different optimisation settings. 它们可以在程序的不同运行中使用不同的值,或者在不同的编译器上编译代码时,或者在使用不同优化设置的同一编译器上编译时。 Or on different days of the week, national holidays or after 6pm. 或者在一周的不同日期,国家法定假日或下午6点之后。

An uninitialised variable can even hold what's called a trap representation, which is a value which is not valid for that type. 未初始化的变量甚至可以保存所谓的陷阱表示,这是一个对该类型无效的值。 If you access such a value then you're into the scary world of undefined behaviour where literally anything can happen. 如果你访问这样一个值,那么你就会陷入未定义行为的可怕世界,其中任何事情都可能发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么将全局变量和静态变量初始化为默认值? - Why are global and static variables initialized to their default values? const 变量在由非 const 和非初始化变量初始化时取什么值? - What value does a const variable take when it's initialized by a non-const and non-initialized variable? 为什么C和C ++编译器将显式初始化和默认初始化全局变量放在不同的段中? - Why do C and C++ compilers place explicitly initialized and default initialized global variables in different segments? 为什么返回未初始化的值被视为未定义的行为? - Why is returning a non-initialized value considered undefined behavior? 静态变量未初始化 - Static variables are not being initialized 当`default:`标签放在大括号外时,复合开关语句中的局部变量是否被初始化? - Are local variables in a compound switch statement initialized when the `default:` label is put outside the braces? 具有非默认类型的函数的返回值 - Return value of a function with a non-default type C 已初始化和未初始化的可变大小数组 - C initialized and non initialized array with variable size 为所有未初始化的变量设置默认值 - set default value for all uninitialized variables 默认情况下全局变量是 extern 吗? 如果是,那么为什么它们具有默认值“ 0 ”? - Are global variables by default extern? if yes, then why they have default value " 0 "?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM