<分区>
今天我在cpp中遇到了一种新的分配类型,我从未听说过我试图搜索谷歌但没有找到任何相关答案。
long int *a=new long int[N+1]();
我知道long int *a = new long int[N+1];
但是以上两者有什么区别呢?
<分区>
今天我在cpp中遇到了一种新的分配类型,我从未听说过我试图搜索谷歌但没有找到任何相关答案。
long int *a=new long int[N+1]();
我知道long int *a = new long int[N+1];
但是以上两者有什么区别呢?
最佳答案
long int *a = new long int[N+1]();
^^
分配内存,initialize them to the default state类型(对于内置类型,零)。这是初始化由 new
分配的对象的标准方法,因此它们不包含不确定的值(虽然没有 UB)。在 C++11 中,您还可以使用大括号:
long int *a = new long int[N+1]{};
^^
关于c++ - C++中的动态分配数组差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47321277/