简体   繁体   English

非静态变量初始化

[英]Non-static variable initialization

I'm reading a book C progaming faq's. 我正在读一本关于C progaming faq的书。 Here is passage of the book 这是本书的篇章

Automatic variables are variables defined inside a function or block of code without the static keyword. 自动变量是在没有static关键字的函数或代码块内定义的变量。 These variables have undefined values if you don't explicitly initialize them. 如果未显式初始化这些变量,则这些变量具有未定义的值。 If you don't initialize an automatic variable, you must make sure you assign to it before using the value. 如果未初始化自动变量,则必须确保在使用该值之前将其分配给它。

Here is my code: 这是我的代码:

#include <stdio.h>
int main (int argc, const char * argv[])
{    
    {
        int x;
        printf("%d", x);
    }
}

The result of printf is 0. Why is the variable initialized? printf的结果为0.为什么变量初始化?

For static and global variables it is 0; 对于静态和全局变量,它为0; automatic variables are not initialized by default. 默认情况下不会初始化自动变量。

in the c language there is no default value for non static local variables. 在c语言中,非静态局部变量没有默认值。 The variable holds whatever was in memory before it became a variable. 变量在成为变量之前保存内存中的内容。 It's best to always initialize a non static local variable before using it in the c language (or at least before comparing it to something else). 最好在使用c语言之前始终初始化非静态局部变量(或者至少在将其与其他东西进行比较之前)。 Also It's best to assume that there is no default value because this varies from language to language, and hardware to hardware. 此外,最好假设没有默认值,因为这会因语言和硬件而异。

Read more: http://wiki.answers.com/Q/What_is_the_default_value_of_integer_in_c#ixzz1iaij7hRK 阅读更多: http//wiki.answers.com/Q/What_is_the_default_value_of_integer_in_c#ixzz1iaij7hRK

It isn't initialized. 它没有初始化。 The memory cell your x occupies still has a value from earlier use. x占用的内存单元仍具有早期使用的值。 However, the value of x might be anything, so you can't rely on it. 但是, x的值可能是任何值,因此您不能依赖它。

Undefined means it could be anything, even 0. Another implementation may have a different or random value. 未定义意味着它可以是任何东西,甚至是0.另一个实现可能具有不同的或随机的值。 There's no way to know, and you can't trust it to be the same on every execution either. 没有办法知道,你也不能相信它在每次执行时都是一样的。

Some compilers will do default initializations for you, some won't. 有些编译器会为你做默认初始化,有些则不会。 You shouldn't count on them. 你不应该指望他们。 The line saying 这条线说

int x;

should say 应该说

int x = 0;

if that's what you want. 如果那是你想要的。 For some C compilers, it's possible that x is -23157263 at the point printf is called. 对于某些C编译器,调用printf时x可能是-23157263。

The initial value of an automatic variable if not explicitly initialized is indeterminate. 如果未显式初始化,则automatic变量的初始值是不确定的。

Indeterminate means either unspecified (could be 0, or 42) or a trap representation. Indeterminate意味着未指定(可能是0或42)或陷阱表示。 A trap means reading the uninitialized variable undefined behavior (for example, crash you program). 陷阱意味着读取未初始化的变量未定义行为(例如,使程序崩溃)。

Because at the memory space where x is placed there is already a 0, but this is just "luck" there could be any number or symbol at that place. 因为在放置x的存储空间中已经有0,但这只是“运气”,那个地方可能有任何数字或符号。 So to be sure that always get the same result you should always initialize a variable. 因此,为了确保始终获得相同的结果,您应该始终初始化变量。

I recall that Visual Studio would (and perhaps still does) set auto variables to 0 or null in some cases when the code was compiled with debug flags. 我记得在使用调试标志编译代码时,Visual Studio会(并且可能仍然)将自动变量设置为0或null。 Like others have said, you certainly should not count on any default values. 像其他人所说,你当然不应该指望任何默认值。

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

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