[英]arrays using pointers in c++: segmentation fault when accessing the returned array
我是C ++的新手,正在嘗試使用指向指針的指針構建3維數組。 我確信這樣做有更有效的方法,但是我現在正在嘗試理解指針。
作為示例代碼,我最初有以下代碼,可以很好地工作,分配,初始化和釋放內存。
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;
}
和我的main()
,我嘗試以無濟於事的方式訪問3d數組。
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);
}
當我對此進行編譯時,“生成器”功能運行良好,但是每當我嘗試訪問main函數中的3d數組時,都會遇到分段錯誤。 我希望它能奏效,因為我在書中已經讀過,如果使用指向函數的指針通過引用傳遞了某些東西,則該函數將具有操縱它的能力。 另外,我希望我需要將3d數組取消引用3次(即*** freq)才能正確訪問元素,但是編譯器因為嘗試這樣做而生氣,並脫口而出
myBuilder.cpp:42:17:錯誤:間接要求指針操作數('int'無效)cout << ***(frequencies [i1] +1)<< endl;
我知道這是一個新問題,但是任何幫助將不勝感激!
的frequencies
在指針builder
和destroyer
是副本 freq
在指針main
。 因此,在builder
中設置它不會更改main
的(未初始化)指針。 您需要引用一個指針:
void builder(int aSize1, int aSize2, int aSize3, int***& frequencies);
並且對於您的間接級別,請注意,如果freq
是int***
,則freq[1]
是int**
。
在代碼中調用builder(aSize1, aSize2, aSize3, freq);
從main()中,您正在傳遞int ***frequencies
(自創建以來就包含垃圾值),並希望在構建器函數調用中更新此三重指針。
構建器函數分配內存並更新下面代碼行中的構建器函數調用中作為參數傳遞的*** frequecny的副本
frequencies = new int** [aSize1+1];
因此,這是對頻率指針的按值調用,在完成對builder的調用之后,不會在main()中更新更新返回該指針。 它仍然包含垃圾地址,該地址會被訪問以引起分段錯誤。
您需要在構建器調用中傳遞頻率指針的地址(例如&frequency),並在構建器函數中進行相應的更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.