使用c ++中的指针的数组:访问返回的数组时的分段错误

Ast*_*oom 0 c++ arrays pointers segmentation-fault

我是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;

}
Run Code Online (Sandbox Code Playgroud)

我想通过将上面的代码分成几个部分来提高赌注,这样我就可以在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;
}
Run Code Online (Sandbox Code Playgroud)

以及我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);
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,"构建器"函数运行正常,但每当我尝试访问main函数中的3d数组时,我都会遇到分段错误.我希望这可以工作,因为我已经在我的书中读到,如果通过引用使用指向函数的指针传递某些东西,该函数将具有操纵它的能力.另外,我预计我需要取消引用3d数组3次(即***freq)才能正确访问这些元素,但是编译器因为试图这样做而生气,并且脱口而出

myBuilder.cpp:42:17:错误:间接需要指针操作数('int'无效)cout <<***(frequency [i1] +1)<< endl;

我知道这是一个新问题,但任何帮助将不胜感激!

asc*_*ler 5

frequencies在指针builderdestroyer一个拷贝的的freq的指针main.因此设置它builder不会改变(未初始化的)指针main.您想要引用指针:

void builder(int aSize1, int aSize2, int aSize3, int***& frequencies);
Run Code Online (Sandbox Code Playgroud)

对于你的间接水平,请注意,如果freqint***,那么freq[1]是一个int**.