C ++中未初始化的变量行为 [英] Uninitialized variable behaviour in C++

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

问题描述

我已经检查过自己,写了这样的程序

I've checked myself, I wrote a program like this

int main() {
 int i;
 cout << i;
 return 0;
}

我运行了几次程序,结果一直都是一样的,零。
我在C语言中尝试过,结果是相同的。

I ran the program a few times and the result was same all the time, zero. I've tried it in C and the result was the same.

但是我的教科书说

如果不初始化在函数内部定义了
的变量,则该变量的值保持未定义状态,这意味着该元素采用
而不管该位置先前的值。在内存中。

If you don’t initialize an variable that’s defined inside a function, the variable value remain undefined.That means the element takes on whatever value previously resided at that location in memory.

当程序始终为变量分配空闲存储位置时,这怎么可能?

How's this possible when the program always assign a free memory location to a variable? How could it be something other than zero(I assume that default free memory value is zero)?

推荐答案


当程序总是将可用内存
位置分配给变量时,这怎么可能?

How's this possible when the program always assign a free memory location to a variable? How could it be something rather than zero?

让我们看一个示例实际实现。

Let's take a look at an example practical implementation.

假设它利用堆栈来保存局部变量。

Let's say it utilizes stack to keep local variables.

void
foo(void)
{
        int foo_var = 42;
}

void
bar(void)
{
        int bar_var;
        printf("%d\n", bar_var);
}

int
main(void)
{
        bar();
        foo();
        bar();
}

上面完全破损的代码说明了这一点。调用foo之后,将在堆栈上放置foo_var的特定位置设置为42。当我们调用bar时,bar_var占据该确切位置。确实,执行代码会导致打印0和42,表明除非进行初始化,否则无法依赖bar_var值。

Totally broken code above illustrates the point. After we call foo, certain location on the stack where foo_var was placed is set to 42. When we call bar, bar_var occupies that exact location. And indeed, executing the code results in printing 0 and 42, showing that bar_var value cannot be relied upon unless initialized.

现在应该很清楚,需要局部变量初始化。但是 main 是一个例外吗?有什么可能在堆栈中起作用,从而给我们一个非零值?

Now it should be clear that local variable initialisation is required. But could main be an exception? Is there anything which could play with the stack and in result give us a non-zero value?

是的。 main不是程序中执行的第一个功能。实际上,要设置所有内容都需要的工作。任何一项工作都可能使用了堆栈,并在堆栈上留下了一些非零值。您不仅不能期望在不同的操作系统上具有相同的值,而且很可能突然改变您当前正在使用的系统。有兴趣的人士可以在Google上搜索动态链接器。

Yes. main is not the first function executed in your program. In fact there is tons of work required to set everything up. Any of this work could have used the stack and leave some non-zeros on it. Not only you can't expect the same value on different operating systems, it may very well suddenly change on the very system you are using right now. Interested parties can google for "dynamic linker".

最后,C语言标准甚至没有术语堆栈。局部变量有一个位置留给编译器。它甚至可能从给定寄存器中发生的任何事情中得到随机的废话。确实可以完全任何事情。实际上,如果触发了未定义的行为,则编译器可以自由执行任何感觉。

Finally, the C language standard does not even have the term stack. Having a "place" for local variables is left to the compiler. It could even get random crap from whatever happened to be in a given register. It really can be totally anything. In fact, if an undefined behaviour is triggered, the compiler has the freedom to do whatever it feels like.

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

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