如何解决C中的默认垃圾值?
如果我们在 C 编程中将变量声明为整数而不定义值,则 printf 会从缓冲区打印一些垃圾值。有什么方法可以防止打印垃圾值吗?
我想检查是否可以在编译时完成某些操作?如果可能的话?
If we declare a variable in the C programming as integer without defining value then printf prints some garbage value from buffer. Is there any method to prevent printing the garbage value?
I want to check if something can be done at compile time? if possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
维基百科的说法如下:
因此将其初始化为默认值。
Wikipedia says the following:
So initialize it to a default value.
如果您正在定义局部变量,则不会。编译器不会为您初始化它们。堆栈的增长和缩小取决于所调用的函数及其局部变量。编译器在每次函数调用时清除所有内存是没有意义的。
但是,如果将全局变量放置在 .bss 这是缩小程序大小的优化。
If you are defining local variables then no. The compiler doesn't initialize them for you. The stack grows and shrinks depending on functions being called and their local variables. It doesn't make sense for a compiler to clear all the memory at every function call.
However, global variables can have an initial value of 0 if they are placed in the .bss this is an optimization to shrink the size of a program.
在编译时,大多数编译器(例如 GNU 编译器)可以识别使用未初始化变量的位置。但是,您可能需要设置标志,例如 GNU 编译器的
-Wall
。变量的值在这里已经,但它可以是任何值。即它是变量的“初始”状态。
因此,您必须初始化该变量以避免产生垃圾。
当声明变量
x
时,它已经为内存分配了一个部分&x
由该变量名引用。未初始化的值和变量已放置在内存地址中。假设您初始化一个 int 类型的变量
v
。它被分配到一个内存地址,即int *类型的&v
。因此,地址&v
将被放置到内存中未使用的开放位置。考虑主函数中的这段代码:
编译此代码时,会出现此警告消息,其中
SOME_DIRECTORY
是任意目录:内存的起始值,就像电路一样,是不可预测的。不管为什么你的值都是随机垃圾。这也是一种未定义行为,这意味着C编译器的国际标准没有设置任何要求,因此任何事情都可能发生。这是一个非常糟糕的错误,可能会导致多个难以追踪的错误和故障。
At compile times, most compilers such as the GNU compiler can identify where an uninitialized variables is used. However, you may need to set flags, such as
-Wall
for the GNU compiler.The value of the variable is already here though it could be any value. Namely it is the "initial" state of the variable.
Therefore, you must initialize the variable to avoid garbage.
When a variable
x
is declared, it already allocates a section to the memory&x
which is referred by this variable name. Uninitialized values and variables are already placed in a memory address.Suppose you initialize a variable
v
of type int. It is allocated to a memory address, which is&v
of type int *. Address&v
therefore would be placed to an open unused place in the memory.Consider this code inside a main function:
When compiling this code, this warning message occurs, where
SOME_DIRECTORY
is an arbitrary directory:The starting value of a memory, just like circuits, is unpredictable. No matter why your value is random garbage. This is also a form of undefined behavior, which means that the International Standard of C compilers do not set any requirements so anything may happen. This is a very bad bug, can cause multiple hard-to-trace bugs and glitches.
您当然希望使用所有警告和调试信息进行编译:
gcc -Wall -Wextra -g
和 海湾合作委员会。那么您很可能会收到警告,并且您应该改进代码以消除警告。当然,您应该初始化变量。顺便说一句,这样的初始化代码很短,并且运行速度非常快。在某些情况下(例如使用
-O1
)gcc
能够优化(使用as-if 规则)并删除无用的初始化。因此,根据经验,不要害怕“无用”的初始化。您应该养成初始化大多数变量的习惯(很少有例外,例如一些 PRNG),实际上是所有这些。在少数情况下,您没有故意初始化变量并希望它保留一些垃圾值,请在注释中记录下来(但不要期望垃圾是真正随机的;实际上它仍然可能始终相同)。
详细了解未定义行为。 害怕 UB。
请记住,变量是源代码中的名称。变量在运行时不存在(此时仅存在位置)。它们可能会被编译器删除,有时可能位于调用堆栈<的调用框架的某个位置中/a>,他们可以进入某个寄存器,等等......
You certainly want to compile with all warnings and debug info:
gcc -Wall -Wextra -g
with GCC. Then it is likely that you'll get warnings, and you should improve your code to get none.Of course, you should initialize your variables. BTW, such an initialization is short to code, and runs very quickly. And in several occasions (with e.g.
-O1
)gcc
is able to optimize (using the as-if rule) and remove useless initializations. So as a rule of thumb don't be scared of "useless" initializations.You should get the habit to initialize most variables (there are very few exceptions, e.g. the seed of some PRNG), actually all of them. In the few cases where you don't initialize a variable on purpose and prefer it to retain some garbage value, document that in a comment (but don't expect that garbage to be really random; it still could in practice be always the same).
Read more about undefined behavior. Be scared of UB.
Remember that variables are names in the source code. Variables don't exist at runtime (only locations exist then). They could be removed by the compiler, they might sometimes sit in some slot of the call frame of the call stack, they could go in some register, etc...
是的。初始化变量。
Yes. Initialise the variable.