C++ 默认初始化会将数组元素设置为其默认值吗?

Will C++ default-initialization set array elements to its default value?

考虑以下代码:

#include <iostream>

using namespace std;

int main(){
  int* p = new int[2];
  for (int i = 0; i < 2; i++)
    cout << p[i] << endl;  
  return 0;
}

我运行它好几次了。它总是产生以下输出:

0
0

我可以假设 C++ 默认初始化将数组元素设置为其默认值吗?在这种情况下,我可以假设 p 的元素值始终设置为 0 吗?

我已阅读以下相关问题。但他们并没有专门针对我的情况:

Can I assume that C++ default-initialization set array elements to its default value?

否,因为 default initialization:

  • if T is an array type, every element of the array is default-initialized;

且元素类型为int,则

  • otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.

另一方面,list initialization(since C++11) like int* p = new int[2]{}; or int* p = new int[2]{0};, or value initializationint* p = new int[2](); 将保证,对于 int 所有元素都将被零初始化。