初始化未初始化值? [英] Uninitialized values being initialized?

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

问题描述

在C,自动变量,如果未初始化,持有垃圾的价值。然而,考虑下面的程序:

In C, automatic variables, if uninitialized, hold a garbage value. However, consider the following program:

int main(){
 signed char term;
 (char)term--;
 printf("%d\n",term);
}

它打印的'7'的值。如果我不这样做(char)的名词 - ,它打印的'8'的值。所以,它绝对不持有垃圾的价值。这不是矛盾吗?

It prints a value of '7'. If I don't do a (char)term--, it prints a value of '8'. So, it definitely doesn't hold a garbage value. Isn't this contradictory?

推荐答案

这是垃圾。你得到8垃圾,并减去得到7。

That's garbage. You get 8 as garbage, and subtract to get 7.

这是不确定的行为是什么。仅仅因为你不断收到8并不意味着它的明确定义。尝试在code做更复杂的事情。添加上方和下方的变量字符

This is what undefined behavior is. Just because you keep getting 8 doesn't mean it's well-defined. Try doing more complex things in your code. Add variables above and below your char.


关于你的测试,你说:

然而,一致性是很难忽视,考虑到垃圾应是随机的。

However, the consistency is hard to overlook, considering that garbage should be random.

您需要检查你的假设。 垃圾应是随机的说:是谁?据又该垃圾是随机的?唯一的办法的垃圾将是随机的是,如果系统周期性经过内存和随机数分配给它。

You need to check your assumptions. "garbage should be random" says who? According to what should the garbage be random? The only way garbage will be random is if the system periodically goes through memory and assigns random numbers to it.

当我们说随机,我们的意思是我们不知道它是什么。这并不能使它不确定性。这些都是计算机。如果你告诉他们一遍又一遍地做同样的事情,它会做同样的事情的遍地

When we say "random", we mean we don't know what it will be. That doesn't make it non-deterministic. These are computers. If you tell them to do the same thing over and over, it will do the same thing over and over.

您的编译器和设置保持生产相同code,它最终给你这些垃圾值。确定性,但你不能依赖此行为:随机

Your compiler and settings keep producing the same code that ends up giving you these garbage values. Deterministic, yet you cannot rely on this behavior: "random".

此外,1-800并不意味着像你这样你拿这一点。 8并不一定指的垃圾,如这样设置你的编译器填补他们8.他的意思是8是一样的垃圾任何其他号码。

Also, 1-800 didn't mean for you to take this like you did. "8" does not necessarily denote garbage, as in the way things are set up your compiler fills them with 8. What he means is 8 is just as garbage as any other number.

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

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