2

I wrote following code to return multidimensional array from pointer function.Input parameter of this function is one dimensional array, output is pointer that point multidimensional array.

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;
 }

I built this program succesfully in Visual Studio 2008.When I debug this code,the program computed result1 pinter variable but during computing result2 in the function here:

R=new double *[height];
for (i=0; i<=height; i++)
    R[i]=new double [width];

Visual Studio give this error:

An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in stdeneme.exe
Additional information: External component has thrown an exception.

Unfortunately I don't know what to do.

2 Answers 2

9

At a glance I see one error

for (i=0;i<=height;i++)
 {
 R[i]=new double [width];
 }

you have allocated R[height] but the loop goes height+1

you should write the loop

for (i=0; i<height; i++)

Another thing I see is that when you want destroy your matrix you write

delete R[k];

but it should be

delete [] R[k];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. I corrected <=height problem and did it < height but the Visual Studio is going on give the same error.In fact,Visual C++ can compute result1.Unfortunately it can't do memory allocation for result2 variable.Do you have a comment about this topic?
3

The <=s are your problem. Valid array indices go from 0 to N-1. Assigning to result1[N] is an access violation - that's the exception it's complaining about.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.