初始化数组时使用(或不使用)括号
Using (or not using) parentheses when initializing an array
在我正在阅读的 C++ 代码中,有一些数组初始化为
int *foo = new int[length];
还有一些喜欢
int *foo = new int[length]();
我的快速实验无法检测到这两者之间的任何区别,但它们是紧挨着彼此使用的。
有什么区别吗?
编辑;因为有人断言第一个应该给出不确定的输出,所以这里是一个显示可疑数量 0 的测试;
[s1208067@hobgoblin testCode]$ cat arrayTest.cc
//Test how array initilization works
#include <iostream>
using namespace std;
int main(){
int length = 30;
//Without parenthsis
int * bar = new int[length];
for(int i=0; i<length; i++) cout << bar[0] << " ";
cout << endl;
//With parenthsis
int * foo = new int[length]();
for(int i=0; i<length; i++) cout << foo[0] << " ";
cout << endl;
return 0;
}
[s1208067@hobgoblin testCode]$ g++ arrayTest.cc
[s1208067@hobgoblin testCode]$ ./a.out
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[s1208067@hobgoblin testCode]$
编辑2;显然这个测试是有缺陷的,不要相信它 - 查看详细信息的答案
这一行default-initializes length
int
s,也就是说你会得到一堆int
s,其值不确定:
int *foo = new int[length];
这一行 value-initializes 他们改为,所以你得到全零:
int *foo = new int[length]();
使用括号保证数组的所有元素都初始化为 0。我刚刚尝试使用以下代码:
#include <iostream>
using namespace std;
int main(int,char*[]){
int* foo = new int[8];
cout << foo << endl;
for(int i = 0; i < 8; i++)
foo[i] = i;
delete[] foo;
foo = new int[8];
cout << foo << endl;
for(int i = 0; i < 8; i++)
cout << foo[i] << '\t';
cout << endl;
delete[] foo;
foo = new int[8]();
cout << foo << endl;
for(int i = 0; i < 8; i++)
cout << foo[i] << '\t';
cout << endl;
delete[] foo;
return 0;
}
当我编译 运行 时,看起来 foo
每次都分配在相同的内存位置(尽管您可能不能依赖它)。上面程序对我的完整输出是:
0x101300900
0x101300900
0 1 2 3 4 5 6 7
0x101300900
0 0 0 0 0 0 0 0
因此,您可以看到 foo
的第二次分配没有触及分配的内存,使其处于与第一次分配时相同的状态。
在我正在阅读的 C++ 代码中,有一些数组初始化为
int *foo = new int[length];
还有一些喜欢
int *foo = new int[length]();
我的快速实验无法检测到这两者之间的任何区别,但它们是紧挨着彼此使用的。
有什么区别吗?
编辑;因为有人断言第一个应该给出不确定的输出,所以这里是一个显示可疑数量 0 的测试;
[s1208067@hobgoblin testCode]$ cat arrayTest.cc
//Test how array initilization works
#include <iostream>
using namespace std;
int main(){
int length = 30;
//Without parenthsis
int * bar = new int[length];
for(int i=0; i<length; i++) cout << bar[0] << " ";
cout << endl;
//With parenthsis
int * foo = new int[length]();
for(int i=0; i<length; i++) cout << foo[0] << " ";
cout << endl;
return 0;
}
[s1208067@hobgoblin testCode]$ g++ arrayTest.cc
[s1208067@hobgoblin testCode]$ ./a.out
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[s1208067@hobgoblin testCode]$
编辑2;显然这个测试是有缺陷的,不要相信它 - 查看详细信息的答案
这一行default-initializes length
int
s,也就是说你会得到一堆int
s,其值不确定:
int *foo = new int[length];
这一行 value-initializes 他们改为,所以你得到全零:
int *foo = new int[length]();
使用括号保证数组的所有元素都初始化为 0。我刚刚尝试使用以下代码:
#include <iostream>
using namespace std;
int main(int,char*[]){
int* foo = new int[8];
cout << foo << endl;
for(int i = 0; i < 8; i++)
foo[i] = i;
delete[] foo;
foo = new int[8];
cout << foo << endl;
for(int i = 0; i < 8; i++)
cout << foo[i] << '\t';
cout << endl;
delete[] foo;
foo = new int[8]();
cout << foo << endl;
for(int i = 0; i < 8; i++)
cout << foo[i] << '\t';
cout << endl;
delete[] foo;
return 0;
}
当我编译 运行 时,看起来 foo
每次都分配在相同的内存位置(尽管您可能不能依赖它)。上面程序对我的完整输出是:
0x101300900
0x101300900
0 1 2 3 4 5 6 7
0x101300900
0 0 0 0 0 0 0 0
因此,您可以看到 foo
的第二次分配没有触及分配的内存,使其处于与第一次分配时相同的状态。