我是C++新手,我正在尝试使用指向指针的指针构建一个三维数组。我相信有更有效的方法可以做到这一点,但我现在正试图理解指针。
作为示例代码,我最初有以下部分,它工作正常,分配,初始化和释放内存。
void builder(int aSize1, int aSize2, int aSize3)
{
int i1, i2, i3;
int ***frequencies;
cout << "allocation started ..." << endl;
frequencies = new int** [aSize1+1];
for (i1=0; i1<=aSize1; i1++){
frequencies[i1] = new int*[aSize2+1];
for (i2 = 0; i2 <= aSize2; i2++)
{
frequencies[i1][i2] = new int [aSize3 + 1];
}
}
cout << "allocation done" << endl;
cout << " " << endl;
cout << "before initialization" << endl;
for (i1=0; i1<=aSize1; i1++){
for(i2=0; i2<=aSize2; i2++){
for(i3 = 0; i3 <= aSize3; i3++)
{
frequencies[i1][i2][i3]= (i1 * i2) % 10;
}
}
}
cout << "after initialization" << endl;
cout << " " << endl;
/* the "destroyer" part */
cout << "deleting ..." << endl;
for (i1=0; i1<=aSize1; i1++){
for(i2=0; i2<=aSize2; i2++){
delete [] frequencies[i1][i2];
}
}
for (i1=0; i1<aSize1; i1++){
delete [] frequencies[i1];
}
delete [] frequencies;
cout << "deleting done" << endl;
}
我想通过将上面的代码分成几个部分来提高赌注,以便我可以在程序的main()
函数中使用初始化的数组(只是为了看看我是否可以在那里访问它们)。所以,我最终做了以下事情
头文件:
void builder(int aSize1, int aSize2, int aSize3, int*** frequencies)
{
int i1, i2, i3;
//int ***frequencies;
cout << "allocation started ..." << endl;
frequencies = new int** [aSize1+1];
for (i1=0; i1<=aSize1; i1++){
frequencies[i1] = new int*[aSize2+1];
for (i2 = 0; i2 <= aSize2; i2++)
{
frequencies[i1][i2] = new int [aSize3 + 1];
}
}
cout << "allocation done" << endl;
cout << " " << endl;
cout << "before initialization" << endl;
for (i1=0; i1<=aSize1; i1++){
for(i2=0; i2<=aSize2; i2++){
for(i3 = 0; i3 <= aSize3; i3++)
{
frequencies[i1][i2][i3]= (i1 * i2) % 10;
}
}
cout << **(frequencies[i1]+2) << endl;
}
cout << "after initialization" << endl;
cout << " " << endl;
}
void destroyer( int aSize1, int aSize2, int aSize3, int*** frequencies )
{
int i1, i2;
cout << "deleting ..." << endl;
for (i1=0; i1<=aSize1; i1++){
for(i2=0; i2<=aSize2; i2++){
delete [] frequencies[i1][i2];
}
}
for (i1=0; i1<aSize1; i1++){
delete [] frequencies[i1];
}
delete [] frequencies;
cout << "deleting done" << endl;
}
以及我尝试徒劳无功地访问 3D 阵列的main()
。
int main()
{
int aSize1 = 10;
int aSize2 = 10;
int aSize3 = 10;
int*** freq;
builder(aSize1, aSize2, aSize3, freq);
cout << "builder finished" << endl;
cout << **(freq[1]+2) << endl;
destroyer( aSize1, aSize2, aSize3, freq);
}
当我编译它时,"构建器"函数运行良好,但是每当我尝试访问主函数中的 3d 数组时,我都会出现分割错误。我希望这能起作用,因为我在我的书中读到过,如果使用指向函数的指针通过引用传递某些东西,则该函数将有能力操纵它。另外,我希望我需要取消引用 3d 数组 3 次(即 ***freq)才能正确访问元素,但编译器因为我试图这样做而生气并脱口而出
myBuilder.cpp:42:17:错误:间接寻址需要指针操作数("int"无效) cout <<***(频率[i1]+1) <<endl;
我知道这是一个新手问题,但任何帮助将不胜感激!
builder
和destroyer
中的frequencies
指针是main
中freq
指针的副本。 因此,在builder
中设置它不会更改main
中的(未初始化)指针。 您需要对指针的引用:
void builder(int aSize1, int aSize2, int aSize3, int***& frequencies);
对于您的间接级别,请注意,如果freq
是int***
,则freq[1]
是int**
。
在代码中,当您从 main() 调用builder(aSize1, aSize2, aSize3, freq);
时,您正在传递int ***frequencies
(自成立以来就包含垃圾值),并希望在生成器函数调用中更新此三重指针。
构建器函数分配内存并更新在下面的代码行中作为生成器函数调用中的参数传递的 ****frequcny 的副本
frequencies = new int** [aSize1+1];
因此,它是频率指针的值调用,在完成对生成器的调用后,不会在 main() 中返回更新。它仍然包含垃圾地址,可以访问这些地址会导致分段错误。
您需要在构建器调用中传递频率指针的地址,例如&频率,并在构建器函数中进行相应的更改。