简体   繁体   English

为什么 `main` function 中的局部 int 变量会自动初始化?

[英]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.

相关问题 在函数中初始化而在main中未初始化 - Initialized in a function and is not initialized in main Visual C ++ 2005-本地int和double变量是否默认初始化为0? - Visual C++ 2005 - are local int and double variables initialized to 0 by default or not? 在主 memory 中的循环内分配局部变量 - Allocation of local variables inside a loop in the main memory C ++可以在函数内部使用局部变量,该函数的类型是从函数返回类型自动推断出来的吗? - Can C++ use local variables inside a function whose type is automatically inferred from the function return type? 为什么在main中声明的变量被初始化为好像是我的类的变量? - Why is a variables that I declared in main being initialized as if it was a variable for my class? 为什么变量“sum”必须在 function 中初始化,而不是在 main 之外? - Why does a variable “sum” have to be initialized within a function but not outside of main? 为什么变量初始化为0? - Why are variables initialized to 0? 如何访问main中函数的变量? - How to access the variables of a function inside main? 为什么在类中声明的变量,在函数中定义的变量可以初始化? - Why variables declared in class, defined in function can be initialized? Int主要在一个类内 - Int main inside a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM