简体   繁体   English

在C中,如果没有为变量赋值,那为什么它会占用垃圾值?

[英]In C if a variable is not assigned a value then why does it take garbage value?

为什么变量采用垃圾值?

I guess the rationale for this is that your program will be faster. 我想这个理由是你的程序会更快。

If compiler automatically reset (ie: initialize to 0 or to NaN for float/doubles etc etc) your variables, it would take some time doing that (it'd have to write to memory). 如果编译器自动重置 (即:初始化为0或NaN为浮点数/双精度等等)你的变量,那么这需要一些时间(它必须写入内存)。
In many cases initializing variables could be unneeded: maybe you will never access your variable, or will write on it the first time you access it. 在许多情况下,初始化变量可能是不需要的:也许你永远不会访问你的变量,或者在你第一次访问变量时会在其上写。

Today this optimization is arguable: the overhead due to initializing variables is maybe not worth the problems caused by variables uninitialized by mistake, but when C has been defined things were different. 今天,这种优化是有争议的:由于初始化变量而产生的开销可能不值得由错误未初始化的变量引起的问题,但是当C被定义时,事情是不同的。

未分配的变量具有所谓的不确定状态,可以以任何方式实现,通常只是保持变量中现在占用的内存中的数据不变。

它只需要在变量所指向的地址处获取内存中的内容。

When you allocate a variable you are allocating some memory. 分配变量时,您正在分配一些内存。 if you dont overwrite it, memory will contain whatever "random" information was there before and that is called garbage value. 如果你不覆盖它,内存将包含之前的任何“随机”信息,这被称为垃圾值。

Why would it not ? 为什么呢? A better question might be "Can you explain how it comes about that a member variable in C# which is not initialised has a known default value?" 一个更好的问题可能是“你能解释一下C#中初始化的成员变量是否具有已知的默认值?”

When variable is declared in C, it involves only assigning memory to variable and no implicit assignment. 当在C中声明变量时,它只涉及将内存分配给变量而不涉及隐式赋值。 Thus when you get value from it, it has what is stored in memory cast to your variable datatype. 因此,当您从中获取值时,它会将内存存储到您的变量数据类型中。 That value we call as garbage value. 我们称之为垃圾值的那个值。 It remains so, because C language implementations have memory management which does not handle this issue. 它仍然如此,因为C语言实现具有内存管理,无法处理此问题。

This happens with local variables and memory allocated from the heap with malloc(). 使用malloc()从堆分配的局部变量和内存会发生这种情况。 Local variables are the more typical mishap. 局部变量是更典型的事故。 They are stored in the stack frame of the function. 它们存储在函数的堆栈框架中。 Which is created simply by adjusting the stack pointer by the amount of storage required for the local variables. 这是通过将堆栈指针调整为局部变量所需的存储量来创建的。

The values those variables will have upon entry of the function is essentially random, whatever happened to be stored in those memory locations from a previous function call that happened to use the same stack area. 这些变量在输入函数时将具有的值基本上是随机的,无论发生在先前函数调用的那些存储器位置中,这些值恰好使用相同的堆栈区域。

It is a nasty source of hard to diagnose bugs. 这是一个难以诊断错误的令人讨厌的来源。 Not in the least because the values aren't really random. 至少因为价值不是真正随机的。 As long as the program has predictable call patterns, it is likely that the initial value repeats well. 只要程序具有可预测的呼叫模式,初始值很可能重复。 A compiler often has a debug feature that lets it inject code in the preamble of the function that initializes all local variables. 编译器通常具有调试功能,允许它在初始化所有局部变量的函数的前导码中注入代码。 A value that's likely to produce bizarre calculation results or a protected mode access violation. 可能产生奇怪计算结果或受保护模式访问冲突的值。

Notable perhaps as well is that managed environments initialize local variables automatically. 值得注意的是,托管环境自动初始化局部变量。 That isn't done to help the programmer fall into the pit of success, it's done because not initializing them is a security hazard. 这样做并不是为了帮助程序员陷入成功之中,这样做是因为没有初始化程序员是一种安全隐患。 It lets code that runs in a sandbox access memory that was written by privileged code. 它允许在沙箱中运行的代码访问由特权代码编写的内存。

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

相关问题 为什么在下面的代码中将垃圾值分配给已声明的变量? - Why is garbage value being assigned to an already declared variable in the code below? c结构如何循环? 为什么变量没有在程序的position(2)处赋值? 它只在 position (1) 中打印? - How does c structure loop? why is the variable not assigned the value at position (2) of the program? it only prints in position (1)? 为什么大的可变长度数组具有固定值-1,即使在C中赋值? - Why does a large variable length array has a fixed value -1 even if assigned to in C? 分配的内存没有分配的值 - Allocated memory does not take value it is assigned 为什么在for循环中将此变量的值更改为垃圾? C - Why is the value of this variable changing to garbage once inside for-loop? C 在C中打印未分配值的变量 - Printing a variable in C that was not assigned a value 为什么C中的此数组不断为其分配垃圾值? - Why does this array in C keeps assigning garbage value to itself? 在C中,如果将双指针分配给值,为什么指针变量为0? - In C if double pointer is assigned to value, why is the pointer variable 0? 为什么即使分配了值,此函数也返回0? (C) - Why does this function return 0 even when assigned a value? (C) 变量值未分配给 C 中的另一个变量 - Variable value not being assigned to another variable in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM