0

I am trying to pass a matrix by reference and then initialize it dynamically. This is just an example:

int main(int argc, char* argv[]) {
  int** matrix = NULL;

  initialize(&matrix, 8);

  return 0;
}

void initialize(int*** matrix, int size) {
  *matrix = (int**) calloc(size, sizeof(int*));

  for (int i = 0; i < size; ++i) {
    *matrix[i] = (int*) calloc(size, sizeof(int));  // cashes here with segmentation fault
  }

  for (int i = 0; i < size; ++i) {
    for (int j = 0; j < size; ++j) {
      *matrix[i][j] = 5; // some number, doesn't matter for now
    }
  }
}

I have also tried the alternative, saving the matrix in a contiguous memory space:

*matrix = (int**) calloc(size, sizeof(int*));

  *matrix[0] = (int*) calloc(size * size, sizeof(int));
  for (int i = 1; i < size; ++i) {
    *matrix[i] = *matrix[0] + size * i; // crashes here, segmentation fault
  }

Yet the same error pops. Never on index 0, always on index 1. I don't understand, what am I doing wrong?

Any kind of help will be greatly appreciated!

Kind regards, Raul.

  • i would suggest that instead of passing the variable matrix to your initialize function to have initialize return the matrix, i find it easier to read. e.g. `int** matrix = initialize(8);` with `int** initialize(int size);` since you are anyway not doing a realloc in your initialize() function .. also maybe `allocateAndInitializeMatrix` would be more appropriate as a function name that describes what the function does. Just my 2c. – AndersK Oct 22 '15 at 05:46
  • Not related to your question since there is a nice answer available below but its worth mentioning that you should not cast the result of malloc or calloc! read here http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Deepanshu Oct 22 '15 at 05:49
  • Yes, that's a good idea, thanks :) –  Oct 22 '15 at 05:53
  • @geeksoul I did that because I am compiling in C. –  Oct 22 '15 at 05:53
  • @MasterKoder my link is validate for C only! In C, you don't need to cast the return value of malloc or calloc. The pointer to void returned by malloc/calloc is automagically converted to the correct type. However, if you want your code to compile with a C++ compiler, a cast is needed. – Deepanshu Oct 22 '15 at 05:56
  • @geeksoul Ahh, mybad, I wasn't aware of that. Thank you very much! :) –  Oct 22 '15 at 05:58

1 Answers1

3
*matrix[i] = (int*) calloc(size, sizeof(int));

is interpreted as:

*(matrix[i]) = (int*) calloc(size, sizeof(int));

That's why you are seeing segmentation fault.

Use:

(*matrix)[i] = (int*) calloc(size, sizeof(int));

You have a similar error in the line:

  *matrix[i][j] = 5;

That should be:

  (*matrix)[i][j] = 5;

You can avoid some of the confusion by using a temporary variable in the function.

void initialize(int*** matrix, int size) {

   int** m = calloc(size, sizeof(int*));

   for (int i = 0; i < size; ++i) {
      m[i] = calloc(size, sizeof(int));
   }

   for (int i = 0; i < size; ++i) {
      for (int j = 0; j < size; ++j) {
         m[i][j] = 5; // some number, doesn't matter for now
      }
   }

   *matrix = m;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270