C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Returning a multidimensional array from pointer function in Visual C++ Posted by yalcin on 29 May 2009 at 6:06 AM
Hi,
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=new double *[height];
for (i=0;i<height;i++)
{
R[i]=new double [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 = new double *[N];
for(i=0;i<N;i++)
{result1[i]=new double [M];}
 
double **result2;
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)
 
// 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:

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 can't understand this error. How can I overcome of this problem?Could you help me please?

Best Regards..



Report
Re: Returning a multidimensional array Posted by Lundin on 29 May 2009 at 7:18 AM
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

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

delete [] R;




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.