简体   繁体   English

初始化未初始化的值?

[英]Uninitialized values being initialized?

In C, automatic variables, if uninitialized, hold a garbage value. 在C中,自动变量(如果未初始化)保存垃圾值。 However, consider the following program: 但是,请考虑以下程序:

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

It prints a value of '7'. 它打印的值为'7'。 If I don't do a (char)term-- , it prints a value of '8'. 如果我不做(char)term-- ,则打印值为'8'。 So, it definitely doesn't hold a garbage value. 所以,它肯定没有垃圾价值。 Isn't this contradictory? 这不矛盾吗?

That's garbage. 那是垃圾。 You get 8 as garbage, and subtract to get 7. 你得到8作为垃圾,减去得到7。

This is what undefined behavior is. 这是未定义的行为。 Just because you keep getting 8 doesn't mean it's well-defined. 仅仅因为你持续获得8并不意味着它的定义很明确。 Try doing more complex things in your code. 尝试在代码中执行更复杂的操作。 Add variables above and below your char . char上方和下方添加变量。


About your "test", you say: 关于你的“测试”,你说:

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 . 如果你告诉他们一遍又一遍地做同样的事情,它会一遍又一遍地做同样的事情。

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". 确定性,但你不能依赖这种行为:“随机”。

Also, 1-800 didn't mean for you to take this like you did. 此外,1-800并不意味着你像你一样采取这个。 "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. “8”并不一定表示垃圾,就像设置事物一样,你的编译器用8填充它们。他的意思是8就像任何其他数字一样垃圾。

值8包含在被认为是“垃圾”或“未初始化”的数字集合中。

Garbage in terms of the value is whatever was left on the stack by some previous function. 就值而言,垃圾是一些先前函数留在堆栈上的任何东西。 Change libc, or any number of things, and that number will change. 更改libc或任何数量的东西,该数字将会改变。

You seem to have gotten the terms "garbage" and "non-deterministic" mixed up. 你似乎已经把“垃圾”和“非确定性”这两个术语搞混了。 The value in term is considered garbage because you have no control over what value is going to be in it.It can change from platform to platform or run to run, ie undefined behavior.. 术语中的值被认为是垃圾,因为您无法控制其中的值。它可以从平台更改为平台或运行运行,即未定义的行为。

On the other hand, if all things in the program's runtime environment are equal, the value will probably be the same for every run. 另一方面,如果程序运行时环境中的所有内容都相等,则每次运行的值可能都相同。 This doesn't preclude it from being garbage, however. 但是,这并不排除它成为垃圾。

It does hold garbage, it holds 8 bits of garbage, and reads the binary as a number from 0-255 (Pretending your using an unsigned char). 它确实持有垃圾,它保存8位垃圾,并从0-255读取二进制数字(假装你使用unsigned char)。

So the memory could have been like this: 所以内存可能是这样的:

| | 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 0 1 0 1 | 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 0 1 0 1 |

And it took the end (1 0 0 1 0 1 0 1) and read it as a number, which is 149. It just depends on what random binary is in the spot of the specified length (ex: unsigned char = 1 byte) 它结束了(1 0 0 1 0 1 0 1)并将其读作数字,即149.它只取决于指定长度的点中的随机二进制数(例如:unsigned char = 1 byte)

If it was an unsigned int, it would take 4 bytes of random garbage and make it a number. 如果它是unsigned int,则需要4个字节的随机垃圾并使其成为一个数字。

Think of it as being initialized with an arbitrary value (bit pattern). 可以将其视为使用任意值(位模式)进行初始化。

That arbitrary value depends on what the stack position for the variable was last used for, it is definitely not a random value. 该任意值取决于变量的堆栈位置最后使用的位置,它绝对不是随机值。 It may or may not be always the same value. 它可能或可能不总是相同的值。 It can depend on anything else that happened before. 它可以取决于之前发生的任何事情。

It is good practice to always initialize automatic variables to avoid undefined behavior. 最好始终初始化自动变量以避免未定义的行为。

Thanks for the answers. 谢谢你的回答。 I tried out a little modification: 我尝试了一点修改:

int main(){
           signed char term1;
           signed char term2;
           signed char term3;
           printf("%d\n%d\n%d\n",term1,term2,term3);
}

I ran the program on 3 different machines. 我在3台不同的机器上运行程序。 The result was the same on all 3 machines: term1 gets value = -124, term2 = 4 and term3 = 8. If I remove term3, term1 = 4, term2 = 8. If I remove term2, term1 = 8. So, basically, the last value to be decalred as signed char gets the value of '8'. 结果在所有3台机器上都相同:term1得到值= -124,term2 = 4,term3 = 8.如果我删除term3,term1 = 4,term2 = 8.如果我删除term2,term1 = 8.所以,基本上,作为signed char去掉的最后一个值得到值'8'。 As 1800 INFORMATION said, '8' is one of the values used to denote garbage, this may be it. 正如1800 INFORMATION所说,'8'是用于表示垃圾的值之一,这可能是它。 However, the consistency is hard to overlook, considering that garbage should be random. 但是,考虑到垃圾应该是随机的,因此很难忽视一致性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM