0

I've been given a code that reads in the matrix from two text files. (Assuming that is correct) I need to come up with a function that multiples two matrices together.

This is the given function prototype:

int** matMult(int **a, int num_rows_a, int num_cols_a, int** b, int num_rows_b, int num_cols_b);

And here is my code for the function:

int** matMult(int **a, int num_rows_a, int num_cols_a, int** b, int num_rows_b, int num_cols_b){

    int **c;

    c = (int**)malloc(sizeof(int*)*num_rows_a);
//    c = calloc(num_rows_a, num_cols_b);


    for (int i = 0; i < num_rows_a; i++) {
        for (int j = 0; j < num_cols_b; j++) {
            int sum = 0;
            for (int k = 0; k < num_cols_a; k++) {

                c[i][j] = a[i][k] * b[k][j] + sum;
                sum = c[i][j]; //so that previous answer gets stored 
            }
        }

    }
    return c;
}

I have to call malloc to allocate space for the resulting matrix, c

Also the issue I'm getting from Xcode is : EXC_BAD_ACCESS

Ravi Dhoriya ツ
  • 4,435
  • 8
  • 37
  • 48
aslbj15
  • 33
  • 2
  • 7
  • You need to allocate more memory; you've allocated the pointers to the rows, but you've not allocated each row. Given that problem, you might need to show your code that allocates the input matrices. In fact, you probably should have a single function that allocates the memory for an NxM matrix, that can be used to allocate each matrix. You might need to think about how you know the size of the returned matrix, too. – Jonathan Leffler Nov 18 '14 at 04:46

2 Answers2

1

You need to allocate memory for columns too. :

int **c;

c = (int**)malloc(sizeof(int*)*num_rows_a);

for(i=0;i<num_rows_a;i++)
    c[i]=malloc(sizeof(int)*num_cols_b);    
Stinson
  • 17
  • 5
  • That needs to be `c[i]=malloc(sizeof(int)*num_cols_b);` – R Sahu Nov 18 '14 at 04:50
  • SHOUT TO YOU GUYS! PERFECT! But can you guys explain why you need to allocate memory for the columns and why did you declare malloc in the first for loop? – aslbj15 Nov 18 '14 at 04:58
  • In your code allocation to rows is done with first malloc statement. but for each row you have to provide memory for second dimension which is done for every row via second malloc statement – Stinson Nov 18 '14 at 05:07
  • @RSahu when working with malloc it is good practice to cast the pointer. – Stinson Nov 18 '14 at 05:08
  • @Stinson, casting returned value of `malloc` is not a good idea. Please see http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc. – R Sahu Nov 18 '14 at 05:11
0

The answer by @Stinson addresses the memory problem already. I'm going to suggest a little refinement of the inner block of code. There is no need to have temporary sum in the code.

for (int i = 0; i < num_rows_a; i++) {
  c[i] = malloc(sizeof(int)*num_cols_b);
  for (int j = 0; j < num_cols_b; j++) {

     // Initialize the element to zero.
     c[i][j] = 0;
     for (int k = 0; k < num_cols_a; k++) {
        // Accumulate the result
        c[i][j] += a[i][k] * b[k][j];
     }
  }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270