1

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

  1. 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;
    
  2. 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

michele
  • 26,348
  • 30
  • 111
  • 168

3 Answers3

1

The loop in C should be

for(i=0; i<=m+2; i++)

Otherwise the first row will be left unallocated.

After this change has been done, the java code is almost the same as the C code. Change malloc to calloc (for zero initialization of the allocated memory) to get the equivalent of the java code.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
0

In C double **a; creates a pointer to a pointer and with a malloc you get a multi-dimensional array.

This post details the reasons why you index from 0: Why Array index start from '0'

This post details why you can't print the size of a dynamically allocated array in C. C, Malloc() and array length

#include <stdlib.h>
#include <stdio.h>

int main() {

    int i,m,n;
    m = 5;
    n = 7;

    double **a;
    a = (double **)malloc((m)*sizeof(double *));
    a[0]=(double*) malloc((m*n)*sizeof(double));
    for(i=1; i<m; i++)
      a[i]=a[i-1] + n;

    double x = 0.123412;
    double y = 3.111222;
    a[1][1] = x;
    a[4][6] = y;

    printf("element in a[1][1] %f \n" , a[1][1]);
    printf("element in a[3][3] %f \n" , a[3][3]);
    printf("element in a[4][6] %f \n" , a[4][6]);
    printf("rows %d \n", m);
    printf("cols %d", n);
}

if you try and access a[5][7] you will get a seg fault as this was not allocated.

The equivalent Java for the data structure is double[][] a = new double[5][7];

Community
  • 1
  • 1
CJW
  • 332
  • 1
  • 14
  • a[0]=(double*) malloc((m*n)*sizeof(double)); I don't understand why m*n...why not n? – michele Jul 25 '15 at 17:49
  • Here it's allocating memory from the heap for the rows. It allocates all the memory up front and then the following lines loop through and assign the pointer to the start of the next row as the position of the last row with the addition of the size of each row n. – CJW Jul 26 '15 at 09:58
  • mmm sorry but I don't understand the difference. If I have to malloc matrix[row][cols]. ...I have edited my answer because I am a bit confused...see if you can. Thanks @crawford-wynnes – michele Jul 26 '15 at 12:09
  • I am not completely sure but I think that we need to allocate all the rows up front. If it was just n then we would only allocate one row. Both solutions above are correct I think. – CJW Jul 26 '15 at 21:36
0

The way I see matrix initialisation in C is this:

1.You allock N blocks-representing the lines
2.THen you go ot each line and say:ok,you are getting M values

I know this is kinda redundant to what you just wrote but when you look at the 2 different ways of initialising a matrix you see the 2 main differences:

  1. malloc,just says here there will be something of this type[even if you cna't really say there are types in C]
  2. calloc,goes one step forward and initialises everything,putting the default value over there - you could see it as the first step closer to JVM

Java,can be seen as a layer on top of the compiler.A layer as in you don;t have to worry about the way it sets a value just that you will have something in that address. One more thing you should always remember about JVM is that a value is no longer there when it is Null [don't forget this little guy here,as you wil lface this issue many many times]

as to your first question: you don;t need to go M+2+1 for the number of lines you should go M+2[last element] -0[first element] +1 ,this is the actual way you calculate the number of elements an array has so that is why your for should start from that position.

Why should you go form 0? Why Array index start from '0' [sorry pasting the same link but it's kind of relevant]

Community
  • 1
  • 1
tudoricc
  • 709
  • 1
  • 12
  • 31