I have this c code:
double **a;
a = (double **)malloc( (m+2+1)*sizeof(double *));
for(i=1; i<=m+2; i++)
a[i] = (double *)malloc( (n+1+1)*sizeof(double));
And I have to convert in java code. I am not sure but, is equivalent to write this in java?:
double[][] a = new double[m+2+1][n+1+1]
What is the best way to convert this data structure in java?
==============================================
debugging the 2 codes I have 2 different behaviors:
m=8 and n=13
Debugging the c code, I am allowed to access up to a[14][407]
int main() {
int i,m,n;
m = 2;
n = 2;
double **a;
a = (double **)malloc( (m+2+1)*sizeof(double *));
for(i=1; i<=m+2; i++)
a[i] = (double *)malloc( (n+1+1)*sizeof(double));
int num_rows = sizeof(a) / sizeof(a[0]);
int num_cols = sizeof(a[0]) / sizeof(a[0][0]);
printf("r %d c %d", num_rows, num_cols);
}
Debugging the java code up to a[10][15]
What's the difference?
Thanks
============================================== FOR @crawford-whynnes
TO DO: Allocate a matrix of rows = r and cols = c
st SOLUTION
double **a; a = (double **)malloc((r)*sizeof(double *)); a[0]=(double*) malloc((r*c)*sizeof(double)); for(i=1; i<r; i++) a[i]=a[i-1] + n;
nd SOLUTION
double **a; a = (double **)malloc( (r)*sizeof(double *)); for(i=0; i<r; i++) a[i] = (double *)malloc( (c)*sizeof(double));
What is the correct solution? FIRST or SECOND?
In second solution I have a matrix of rows = r and cols = r*c
and not a matrix of rows = r and cols = c
...it's correct?
Thanks