从C++ - CLI中的指针函数返回一个多维数组

yal*_*cin 2 c++-cli visual-studio-2008 visual-c++

我编写了以下代码从指针函数返回多维数组.此函数的输入参数是一维数组,输出是指向多维数组的指针.

double  **function( array< double>^ data,int width,int height ) {
    int i;
    double **R = new double *[height];
    for (i=0;i<=height;i++)
        R[i]=new double [width];

    // ....

    return R;
}

int main( void ) {
    int M=2, N=10, i,j;
    // define multimensional array 2x10

    array< array< double >^ >^ input = gcnew array< array< double >^ >(M);

    for (j=0; j<input->Length; j++) {
        input[j]=gcnew array<double>(N);}
        double **result1 = new double *[N];
        for(i=0; i<=N; i++)
            result1[i]=new double [M];
        double **result2 = new double *[N];
        for(i=0; i<=N; i++)
            result2[i]=new double [M];

        //............

        // send first row array of multidimensional array to function

        result1=function(input[0],M,N);

        // send second row array of multidimensional array to function

        result2=function(input[1],M,N);

        for (i=0;i<=N;i++)
            delete R[k];
        delete R;}*/

    return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我在Visual Studio 2008中成功构建了这个程序.当我调试这段代码时,程序计算了result1 pinter变量但是在这里函数的计算结果2中:

R=new double *[height];
for (i=0; i<=height; i++)
    R[i]=new double [width];
Run Code Online (Sandbox Code Playgroud)

Visual Studio提供此错误:

stdeneme.exe中发生未处理的类型'System.Runtime.InteropServices.SEHException'的异常
附加信息:外部组件引发了异常.

不幸的是我不知道该怎么办.

And*_*rsK 9

我一眼就看出一个错误

for (i=0;i<=height;i++)
 {
 R[i]=new double [width];
 }
Run Code Online (Sandbox Code Playgroud)

你已经分配了R [height]但循环高度为+ 1

你应该写循环

for (i=0; i<height; i++)
Run Code Online (Sandbox Code Playgroud)

我看到的另一件事是,当你想破坏你写的矩阵时

delete R[k];
Run Code Online (Sandbox Code Playgroud)

但它应该是

delete [] R[k];
Run Code Online (Sandbox Code Playgroud)