c ++中使用指针的数组:访问返回的数组时出现分段错误 [英] arrays using pointers in c++: segmentation fault when accessing the returned array
问题描述
我是C ++的新手,我正在尝试使用指向指针的指针来构建3维数组.我敢肯定,这样做有更有效的方法,但是我现在正在尝试理解指针.
I'm new to C++ and I'm trying to build a 3 dimensional array using a pointer to a pointer to a pointer. I am sure there are more efficient ways in doing so, but I am trying to understand pointers at the moment.
作为示例代码,我最初有以下代码,可以很好地工作,分配,初始化和释放内存.
As example code, I originally had the following piece, which worked fine, allocating, initializing, and releasing the memory.
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()
函数中使用初始化的数组(只是看是否也可以在其中访问它们).因此,我最终完成了以下操作
I wanted to up the ante by splitting the code above into several parts, so that I could use the initialized array in the main()
function of my program (just to see if I can access them there as well). So, I ended up doing the following
头文件:
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;
}
和我的main()
,在这里我尝试不费吹灰之力地访问3d数组.
and my main()
where I try to access the 3d array in a fruitless effort.
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);
}
当我对此进行编译时,"builder"函数运行良好,但是每当我尝试访问main函数中的3d数组时,都会遇到分段错误.我希望它能奏效,因为我在书中已经读过它,如果使用指向函数的指针通过引用传递了某些东西,则该函数将具有操纵它的能力.另外,我希望我需要将3d数组取消引用3次(即*** freq)才能正确访问元素,但是编译器因为尝试这样做并脱口而出发怒
When I compile this, the "builder" function runs fine, but I get a segmentation fault whenever I try to access the 3d array in the main function. I would expect this to work because I have read it in my book that if something is passed by reference using a pointer to a function, the function would have the power to manipulate it. Also, I expected that I would need to dereference the 3d array 3 times (i.e. ***freq) in order to correctly access the elements, but the compiler gets mad at me for trying to do so and blurts out
myBuilder.cpp:42:17:错误:间接需要指针操作数('int'无效) cout<< ***(频率[i1] +1)<< endl;
myBuilder.cpp:42:17: error: indirection requires pointer operand ('int' invalid) cout << ***(frequencies[i1]+1) << endl;
我知道这是一个新问题,但是任何帮助将不胜感激!
I know this is a newb question, but any help will be appreciated!
推荐答案
builder
和destroyer
中的frequencies
指针是main
中freq
指针的副本 .因此,在builder
中进行设置不会更改main
中的(未初始化)指针.您需要引用指针:
The frequencies
pointer in builder
and destroyer
is a copy of the freq
pointer in main
. So setting it in builder
does not change the (uninitialized) pointer in main
. You want a reference to pointer instead:
void builder(int aSize1, int aSize2, int aSize3, int***& frequencies);
对于间接访问级别,请注意,如果freq
是int***
,则freq[1]
是int**
.
And for your levels of indirection, note that if freq
is an int***
, then freq[1]
is an int**
.
这篇关于c ++中使用指针的数组:访问返回的数组时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!