这个变量将如何初始化?

How will this variable be initialized?

我有以下

char mem_pool[1024*1024*64]; 

int main() {
    // ... 
}

我正试图彻底了解 mem_pool 将如何初始化。经过大量搜索,我的结论是:

有人可以帮助我消除语言所保证的歧义并在我错的时候纠正我吗?

我还想过做以下任何事情

char mem_pool[1024*1024*64] {}; 
char mem_pool[1024*1024*64] = ""; 

我怀疑这是一种 better/recommended 做法,但现在我需要了解我最初的问题。

你的理解是正确的。

数组的元素将全部初始化为零,因为数组具有静态存储持续时间:

[C++11: 3.6.2/2]: Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place. [..]

[C++11: 8.5/5]: To zero-initialize an object or reference of type T means:

  • if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;
  • if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits;
  • if T is a (possibly cv-qualified) union type, the object’s first non-static named data member is zero-initialized and padding is initialized to zero bits;
  • if T is an array type, each element is zero-initialized;
  • if T is a reference type, no initialization is performed.

如果它没有静态存储持续时间,则元素都将具有不确定的值:

[C++11: 8.5/11]: If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [..]

[C++11: 8.5/6]: To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, no initialization is performed.