[英]Why local int variables inside `main` function will be initialized automatically?
I'm new to C++ and have that problem while learning C++.我是 C++ 的新手,在学习 C++ 时遇到了这个问题。
Here is the code这是代码
#include <iostream>
using namespace std;
void another_func() {
int a;
cout << a << endl;
}
int main() {
int a;
cout << a << endl;
another_func();
}
I'm using g++ (GCC) 10.1.0
and I found that everytime when I run the code, the a
inside the main
function would be initialize to 0
, while a
in another_func
would be a random number.我正在使用
g++ (GCC) 10.1.0
,我发现每次运行代码时, main
function 中的a
将被初始化为0
,而another_func
中的a
将是一个随机数。 As follows,如下,
➤ g++ test.cpp && ./a.out
a in main: 0
a in another_func: 32612
As I know, local variables are storaged in the stack and they don't have auto-initialization mechanism.据我所知,局部变量存储在堆栈中,它们没有自动初始化机制。 So the
a
in another_func
is expected.所以
another_func
中的a
是预期的。 However, can someone tell me why the a
in main
function was initialized to 0
?但是,有人能告诉我为什么
main
function 中的a
被初始化为0
吗?
Thanks in advance!提前致谢!
Uninitialised doesn't mean non-zero, it could have any value.未初始化并不意味着非零,它可以有任何值。 On many OSs freshly allocated memory pages are filled with
0
so in non-debug code uninitialised values are often 0
too.在许多操作系统上,新分配的 memory 页面都填充了
0
,因此在非调试代码中,未初始化的值通常也为0
。
The behaviour of your program is undefined but what is likely happening is that a
in main
is either the first use of the stack or you just get lucky and the initialisation code that runs before main
leaves that area of the stack 0
.您的程序的行为是未定义的,但可能发生的情况是
a
in main
是堆栈的第一次使用,或者您很幸运,并且在main
离开堆栈的该区域0
之前运行的初始化代码。
The call to cout
will write to the stack so when you then execute another_func
the stack memory wont be all 0
anymore.对
cout
的调用将写入堆栈,因此当您执行another_func
时,堆栈 memory 将不再全0
。
tl;dr: You must not read uninitialized values. tl; dr:您不得读取未初始化的值。
Both a
s weren't initialized to anything.两个
a
都没有初始化为任何东西。 That means they have an indeterminate value, which could be anything including 0. Any reasoning must stop here because reading from an uninitialized value invokes undefined behaviour (UB).这意味着它们有一个不确定的值,可以是包括 0 在内的任何值。任何推理都必须在这里停止,因为从未初始化的值中读取会调用未定义的行为 (UB)。
UB means anything can happen. UB 意味着任何事情都可能发生。 Your program is invalid and you have no guarantees about anything anymore.
您的程序无效,您不再有任何保证。 You're not allowed to read
a
, so reasoning about why you might read a specific value in a specific situation is not useful.您不允许阅读
a
,因此推理为什么您可能会在特定情况下阅读特定值是没有用的。
That's the point of view of the C++ language.这就是 C++ 语言的观点。 For writing C++ programs that's usually the point of view you need to adopt for yourself.
对于编写 C++ 程序,这通常是您需要自己采用的观点。 Of course there's a compiler involved.
当然,这涉及到一个编译器。 More specifically, a certain compiler in a certain version with a certain configuration compiling this exact piece of code for a specific platform.
更具体地说,具有特定配置的特定版本中的特定编译器为特定平台编译这段确切的代码。 Including all of that you can do some investigation about why you see a specific result.
包括所有这些,您可以对为什么看到特定结果进行一些调查。 If you're interested in how compilers work, that's a useful thing to do.
如果您对编译器的工作方式感兴趣,那将是一件很有用的事情。 But it rarely helps with writing programs.
但它很少有助于编写程序。 Change a compiler flag or use a slightly different piece of code, and the result might be different.
更改编译器标志或使用稍微不同的代码,结果可能会有所不同。
The standard does not require default zero values for local variables, but some compilers might do zero initialization该标准不要求局部变量的默认零值,但一些编译器可能会进行零初始化
Only global
variables and static
members must be initialized to zero unless explicitly initialized otherwise.只有
global
变量和static
成员必须初始化为零,除非另有明确初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.