以三维数组(N*N*N)为例
方法1:
分配
int (*a)[N][N] = new int [N][N][N];
释放
delete [] a;
a = NULL;
方法2:
分配
int *** a = new int ** [N];
for(int i = 0; i < N; ++i){
a[i] = new int * [N];
for(int j = 0; j < N; ++j)
a[i][j] = new int[N];
}
释放
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j)
delete [] (a[i][j]);
delete []a[i];
}
delete [] a;
a = NULL;
效率:实验表明方法1较方法2快,原因大概是new operator的调用开销大,且其开销以调用次数为主要衡量标准