from c malloc matrix to java array -
i have c code:
double **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 have convert in java code. not sure but, equivalent write in java?:
double[][] = new double[m+2+1][n+1+1]
what best way convert data structure in java?
==============================================
debugging 2 codes have 2 different behaviors:
m=8 , n=13
debugging c code, allowed access a[14][407]
int main() { int i,m,n; m = 2; n = 2; double **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 java code a[10][15]
what's difference?
thanks
============================================== @crawford-whynnes
to do: allocate matrix of rows = r , cols = c
st solution
double **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; = (double **)malloc( (r)*sizeof(double *)); for(i=0; i<r; i++) a[i] = (double *)malloc( (c)*sizeof(double));
what correct solution? first or second?
in second solution have matrix of rows = r , cols = r*c
, not matrix of rows = r , cols = c
...it's correct?
thanks
the loop in c should be
for(i=0; i<=m+2; i++)
otherwise first row left unallocated.
after change has been done, java code same c code. change malloc
calloc
(for 0 initialization of allocated memory) equivalent of java code.
Comments
Post a Comment