简体   繁体   English

为什么我的 C++ 代码不声明一个(本地)int 数组而不做任何初始化为零的事情?

[英]Why does not my code in C++ that declares an (local) int array without doing anything initialize by zeros?

I have two C++ codes in which the one has an global int array while the other code has a local array whose length is determined by user input (so at runtime).我有两个 C++ 代码,其中一个有一个全局 int 数组,而另一个代码有一个本地数组,其长度由用户输入决定(因此在运行时)。 Both arrays are not explicitly initialized.两个数组都没有显式初始化。

#include <iostream>
using namespace std;
int M = 1000;
int a[M];
int main() {
  for(int i = 0; i < M; i++)
    cout << a[i];
  cout << endl; 
}
#include <iostream>
using namespace std;
int main() {
  int M;
  cin >> M;
  int a[M];
  for(int i = 0; i < M; i++)
    cout << a[i];
  cout << endl; 
}

I observed that the global array is filled with zeros, while the local array (whose length is determined at runtime) is not filled with zeros and instead filled with a random numbers (but same at a time).我观察到全局数组用零填充,而本地数组(其长度在运行时确定)没有用零填充,而是用随机数填充(但一次相同)。 I used the g++ compiler.我使用了 g++ 编译器。

What is this behavior?这是什么行为? Does C++ standard define this behavior? C++ 标准是否定义了这种行为?

What is this behavior?这是什么行为?

The behaviour is that objects with static storage duration are zero-initialised before any other initialisation (if any).行为是具有静态存储持续时间的对象在任何其他初始化(如果有)之前被零初始化。

The behaviour for all other storage duration is that there is no additional zero initialisation.所有其他存储持续时间的行为是没有额外的零初始化。

Does C++ standard define this behavior? C++ 标准是否定义了这种行为?

Yes, the zero initialisation of static objects is defined in the standard.是的,标准中定义了静态对象的零初始化。 The behaviour of reading indeterminate values is specified to be undefined.读取不确定值的行为被指定为未定义。

Both programs are ill-formed because the size of an (non dynamic) array must be compile time constant, which M is not.这两个程序都是病态的,因为(非动态)数组的大小必须是编译时常量,而M不是。

From the C++ 17 Standard (11.6 Initializers)来自 C++ 17 标准(11.6 初始化程序)

12 If no initializer is specified for an object, the object is default-initialized. 12 如果没有为对象指定初始值设定项,则该对象是默认初始化的。 When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced (8.18).当获得具有自动或动态存储期限的对象的存储时,该对象具有不确定值,如果没有对该对象执行初始化,则该对象保留不确定值直到该值被替换(8.18)。 [ Note: Objects with static or thread storage duration are zero-initialized, see 6.6.2. [ 注意:具有静态或线程存储持续时间的对象是零初始化的,参见 6.6.2。 — end note ] — 尾注 ]

So the array declared in the global namespace has the static storage duration and zero-initialized所以在全局命名空间中声明的数组具有静态存储持续时间和零初始化

While the array declared in main has the automatic storage duration and has indeterminate values of its elements.而在 main 中声明的数组具有自动存储持续时间并且其元素的值不确定。

Pay attention to that the variable length arrays is not a standard C++ feature.请注意,可变长度数组不是标准的 C++ 特性。

int M = 1000;
int a[M];

or或者

cin >> M;
int a[M];

That is the both programs are not standard compliant.那就是这两个程序都不符合标准。

And moreover variable length arrays may not be explicitly initialized in a declaration.此外,可变长度数组可能不会在声明中显式初始化。

They can be supported by some compilers as their own language extensions.一些编译器可以支持它们作为它们自己的语言扩展。

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

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