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;
//define returning variable that point multidimensional array
double **R;
// memory allocation for R
R=newdouble *[height];
for (i=0;i<height;i++)
{
R[i]=newdouble [width];
}
// ....
return R;
}
int main( void ) {
int M=2;
int N=10;
int 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);}
// define result1 and result 2 pointers and memory allocate for these variables
double **result1;
result1 = newdouble *[N];
for(i=0;i<N;i++)
{result1[i]=newdouble [M];}
double **result2;
result2 = newdouble *[N];
for(i=0;i<N;i++)
{result2[i]=newdouble [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)
// 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 result1 but during computing result2 in the function here:
1 2 3 4 5
R=newdouble *[height];
for (i=0;i<height;i++)
{
R[i]=newdouble [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 can't understand this error. How can I overcome of this problem?Could you help me please?
I don't understand why you allocate memory for result2 in main and then call the function function to allocate the multi dimensional array again. result2 is then overwritten resulting in a memory leak.
Also, what is an array class? Is that something that you developed? What is with the ^ symbol in your function decl? What is the point of passing the array object to the function function if you are just building an empty 2d array on the heap and returning the pointer? If you have an ancy fancy template design used to create 1 or 2 dimensional arrays, why do you need the function function at all? It doesn't make any sense. I suspect, from seeing all of the //....... in your post that you aren't showing us everything so there is no way for anyone to help you. the last part of your main shouldn't even compile because there is no R defined within the scope of main.
Let this post be a lesson to other noobies. If you want your post answered successfully, don't follow this pattern!