Returning a multidimensional array from pointer function in Visual C++

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.

[code]double **function( array< double>^ data,int width,int height )

{int i;
//define returning variable that point multidimensional array
double **R;
// memory allocation for R
R=new double *[height];
for (i=0;i^ >^ input = gcnew array< array< double >^ >(M);

for (j=0;jLength;j++){
input[j]=gcnew array(N);}

// define result1 and result 2 pointers and memory allocate for these variables

double **result1;
result1 = new double *[N];
{result1[i]=new double [M];}

double **result2;
result2 = new double *[N];
{result2[i]=new double [M];}


// send first row array of multidimensional array to function


// send second row array of multidimensional array to function


// delete result1 and result2
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 [b]result1[/b] but during computing [b]result2[/b] in the function here:

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

Visual Studio give this error:

[b]An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in stdeneme.exe Additional information: External component has thrown an exception.[/b]
Unfortunately I can't understand this error. How can I overcome of this problem?Could you help me please?

Best Regards..


  • I don't know what's causing the error. Out of memory seems far-fetched. It is more likely some non-standard VC++ oddity.

    But I noticed that you have a severe memory leak here:

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

    This should be

    [code]for (i=0; i<N; i++)
    delete [] R[k];

    delete [] R;

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion