未初始化的变量的值是多少?(复制)

时间:2022-11-27 23:40:37

This question already has an answer here:

这个问题已经有了答案:

Possible Duplicate:
Is uninitialized data behavior well specified?

可能的副本:未初始化的数据行为是否被很好地指定?

I tried the following code

我尝试了以下代码

#include<stdio.h>
void main()
{
int i; \
printf('%d',i);
}

The result gave garbage value in VC++, while same in tc was zero. What will be the correct value? Will an uninitialized variable by default have value of zero? or it will contain garbage value?

该结果在vc++中提供了垃圾值,而在tc中则为零。什么才是正确的价值?默认情况下,未初始化的变量的值是否为零?或者它将包含垃圾值?

Next is on the same

下一个是相同的。

#include<stdio.h> 
void main()
{
int i,j,num;
j=(num>0?0:num*num);
printf("\n%d",j);
}

What will be the output of the code above?

上面代码的输出是什么?

6 个解决方案

#1


8  

Technically, the value of an uninitialized non static local variable is Indeterminate[Ref 1].
In short it can be anything. Accessing such a uninitialized variable leads to an Undefined Behavior.[Ref 2]

从技术上讲,未初始化的非静态局部变量的值是不确定的[Ref 1]。简而言之,它可以是任何东西。访问这样一个未初始化的变量会导致一个未定义的行为。(参考2)

[Ref 1]
C99 section 6.7.8 Initialization:

[Ref 1] C99第6.7.8节初始化:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

如果具有自动存储持续时间的对象没有显式初始化,则其值是不确定的。

[Ref 2]

(参考2)

C99 section 3.18 Undefined behavior:

C99第3.18节未定义行为:

behavior, upon use of a nonportable or erroneous program construct, of erroneous data, or of indeterminately valued objects, for which this International Standard imposes no requirements.

在使用不可移植或错误的程序构造、错误数据或不确定值对象时的行为,本国际标准对此没有要求。

Note: Emphasis mine.

注意:强调我的。

#2


4  

Accessing an unitialized variable is undefined behavior in both C and C++, so reading any value is possible.

在C和c++中,访问统一化的变量都是未定义的行为,因此可以读取任何值。

It is also possible that your program crashes: once you get into undefined behavior territory, all bets are off1.

你的程序也有可能崩溃:一旦你进入了未定义的行为区域,所有的赌注都是错的。


1 I have never seen a program crashing over accessing an uninitalized variable, unless it's a pointer.

我从来没有见过一个程序在访问uninitalize变量时崩溃,除非它是一个指针。

#3


1  

It's indeterminate. The compiler can do what it wants.

这是不确定的。编译器可以做它想做的事。

#4


1  

The value is indeterminate; using the variable before initialization results in undefined behavior.

的值是不确定的;在初始化之前使用变量会导致未定义的行为。

#5


-1  

It's undefined. It might be different between different compilers, different operating systems, different runs of the program, anything. It might not even be a particular value: the compiler is allowed to do whatever it likes to this code, because the effect isn't defined. It might choose to optimize away your whole program. It might even choose to replace your program with one that installers a keylogger and steals all of your online banking login details.

这是未定义的。不同的编译器,不同的操作系统,不同的程序运行,任何东西都可能不同。它甚至可能不是一个特定的值:编译器可以对这段代码做它想做的任何事情,因为效果没有定义。它可能会选择优化整个程序。它甚至可以选择用一个安装键盘记录程序并窃取您的所有在线银行登录信息的程序来替换您的程序。

If you want to know the value, the only way is to set it.

如果你想知道这个值,唯一的方法就是设置它。

#6


-1  

As others have noted, the value can be anything.

正如其他人所指出的,价值可以是任何东西。

This sometimes leads to hard-to-find bugs, e.g. because you happen to get one value in a debug build and get a different value in a release build, or the initial value that you get depends on previous program execution.

这有时会导致难以找到的bug,例如,因为您碰巧在调试构建中获得一个值,在发布构建中获得一个不同的值,或者您得到的初始值取决于以前的程序执行。

Lesson: ALWAYS initialize variables. There's a reason that C# defines values for fields and requires initialization for local variables.

教训:总是初始化变量。c#为字段定义值,并要求对局部变量进行初始化,这是有原因的。

#1


8  

Technically, the value of an uninitialized non static local variable is Indeterminate[Ref 1].
In short it can be anything. Accessing such a uninitialized variable leads to an Undefined Behavior.[Ref 2]

从技术上讲,未初始化的非静态局部变量的值是不确定的[Ref 1]。简而言之,它可以是任何东西。访问这样一个未初始化的变量会导致一个未定义的行为。(参考2)

[Ref 1]
C99 section 6.7.8 Initialization:

[Ref 1] C99第6.7.8节初始化:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

如果具有自动存储持续时间的对象没有显式初始化,则其值是不确定的。

[Ref 2]

(参考2)

C99 section 3.18 Undefined behavior:

C99第3.18节未定义行为:

behavior, upon use of a nonportable or erroneous program construct, of erroneous data, or of indeterminately valued objects, for which this International Standard imposes no requirements.

在使用不可移植或错误的程序构造、错误数据或不确定值对象时的行为,本国际标准对此没有要求。

Note: Emphasis mine.

注意:强调我的。

#2


4  

Accessing an unitialized variable is undefined behavior in both C and C++, so reading any value is possible.

在C和c++中,访问统一化的变量都是未定义的行为,因此可以读取任何值。

It is also possible that your program crashes: once you get into undefined behavior territory, all bets are off1.

你的程序也有可能崩溃:一旦你进入了未定义的行为区域,所有的赌注都是错的。


1 I have never seen a program crashing over accessing an uninitalized variable, unless it's a pointer.

我从来没有见过一个程序在访问uninitalize变量时崩溃,除非它是一个指针。

#3


1  

It's indeterminate. The compiler can do what it wants.

这是不确定的。编译器可以做它想做的事。

#4


1  

The value is indeterminate; using the variable before initialization results in undefined behavior.

的值是不确定的;在初始化之前使用变量会导致未定义的行为。

#5


-1  

It's undefined. It might be different between different compilers, different operating systems, different runs of the program, anything. It might not even be a particular value: the compiler is allowed to do whatever it likes to this code, because the effect isn't defined. It might choose to optimize away your whole program. It might even choose to replace your program with one that installers a keylogger and steals all of your online banking login details.

这是未定义的。不同的编译器,不同的操作系统,不同的程序运行,任何东西都可能不同。它甚至可能不是一个特定的值:编译器可以对这段代码做它想做的任何事情,因为效果没有定义。它可能会选择优化整个程序。它甚至可以选择用一个安装键盘记录程序并窃取您的所有在线银行登录信息的程序来替换您的程序。

If you want to know the value, the only way is to set it.

如果你想知道这个值,唯一的方法就是设置它。

#6


-1  

As others have noted, the value can be anything.

正如其他人所指出的,价值可以是任何东西。

This sometimes leads to hard-to-find bugs, e.g. because you happen to get one value in a debug build and get a different value in a release build, or the initial value that you get depends on previous program execution.

这有时会导致难以找到的bug,例如,因为您碰巧在调试构建中获得一个值,在发布构建中获得一个不同的值,或者您得到的初始值取决于以前的程序执行。

Lesson: ALWAYS initialize variables. There's a reason that C# defines values for fields and requires initialization for local variables.

教训:总是初始化变量。c#为字段定义值,并要求对局部变量进行初始化,这是有原因的。