int **array segmentation fault

Hello everyone,

I am trying to set the values in a dynamically allocated 2-D array like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
*  part1.c
*
*  @author Raul Butuc
*  @version 1.0.0  21/10/2015
*/

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

typedef enum { BLACK, WHITE } color;

void initialize(int***, int);
void printBoard(int**, int);

int main(int argc, char* argv[]) {
  int** firstArray = NULL;
  int** secondArray = NULL;
  int** thirdArray = NULL;
  int** fourthArray = NULL;
  int** fifthArray = NULL;

  initialize(&firstArray, 4);
  initialize(&secondArray, 5);
  initialize(&thirdArray, 6);
  initialize(&fourthArray, 7);
  initialize(&fifthArray, 8);

  //printBoard(firstArray, 4);
  //printBoard(secondArray, 5);
  //printBoard(thirdArray, 6);
  //printBoard(fourthArray, 7);
  //printBoard(fifthArray, 8);

  system("pause");
  return 0;
}

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

  // If I keep these 3 lines, I get an error on the calloc method call
  // Interestingly enough, it runs for i = 0, but crashes for i = 1 (first size passed is 4)
  //for (int i = 0; i < size; ++i) {
  //  *anArray[i] = (int*) calloc(size, sizeof(int));
  //}

  int** ptr = *anArray;
  for (int i = 0; i < size; ++i, ++ptr) {
    int* row = *ptr;
    for (int j = 0; j < size; ++j, ++row) {
      *row = 324; // some value, eventually I will use the WHITE/BLACK enum based on some if/else statements
    }
  }

}

void printBoard(int** anArray, int size) {
  for (int i = 0; i < size; ++i) {
    for (int j = 0; j < size; ++j) {
      if (anArray[i][j] == WHITE) {
        printf("W");
      }
      else {
        printf("B");
      }
    }
    printf("\n");
  }
}


I am not sure I understand how these 2D pointers work, but I am unable to get the value assigned. The error is on the line *row = 324;, which is line 52 (because I have not initialized the rows in the for-loop that I have commented out, reason being that the for-loop itself would cause a segmentation fault when reaching the calloc call).

Any kind of help will be much appreciated.

PS: I am aware that there are other alternatives, such as using an array declaration like int* anArray[] or even by using a uni-dimensional array int *anArray and then have a relationship like anArray[i*row+j], but I would much rather prefer to do it the way I started it.

PS 2: This is just a test file, the actual program is supposed to take input dynamically during runtime, so that's why I need the arrays to be declared as they are.

Kind regards,
Raul.
Last edited on
Okay, I figured out how to correctly do the calloc initialization of the matrix, however, it still crashes with segmentation fault when trying to assign the actual values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void initialize(int*** anArray, int size) {
  *anArray = (int**) calloc(size * size, sizeof(int*));
  int** ptr = *anArray;
  int* row = *ptr;

  for (int i = 0; i < size; ++i, ++row) {
    row = (int*) calloc(size, sizeof(int));
  }

  ptr = *anArray;
  for (int i = 0; i < size; ++i, ++ptr) {
    row = *ptr;
    for (int j = 0; j < size; ++j, ++row) {
      *row = 342;  // currently crashes here
    }
  }
}
Last edited on
1
2
3
  for (int i = 0; i < size; ++i, ++row) {
    row = (int*) calloc(size, sizeof(int)); //leak
  }
that loop is simply losing memory.
(if you run your code through a debugger you'll realize that when you try *row = 342; `row' is pointing to null)

perhaps you intended to do ptr[i] = (int*) calloc(size, sizeof(int));
however you are doing *anArray = (int**) calloc(size * size, sizeof(int*));
you allocate size x size rows ¿?

(by the way, I think that `anArray' is a terrible name for a matrix)

1
2
3
4
5
6
7
8
9
10
int **matrix = (int**) calloc(size, sizeof(int *)); //this will have pointers to the rows

/*for a contiguous block of memory*/
matrix[0] = (int*) calloc(size*size, sizeof(int)); //allocates the whole matrix in one chunk
for(int K=1; K<size; ++K)
   matrix[K] = matrix[0] + size*K; //adjust the rows to point to their region

/* if you don't care if the memory is contiguos */
for(int K=0; K<size; ++K) //for each row
   matrix[K] = (int*) calloc(size, sizeof(int)); //allocate what it needs 
Last edited on
Ahh, yes, that's right. I wasn't 100% sure about what I was actually doing with all the pointers over there. And I was going to change the naming (I actually copied this code from a previous part where I was using a one-dimensional array with the name anArray).

Thank you so so much! Really appreciated.

Kind regards,
Raul.
Apparently it still crashes. The current code is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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] = 342;
    }
  }
}


This doesn't really make much sense, but I think it might have to do with *matrix[i] (or at least that's where it crashes for some reason)
Last edited on
Solved. In case anyone wonders, had to replace *matrix[i] = (int*) calloc(size, sizeof(int)); with (*matrix)[i] = (int*) calloc(size, sizeof(int));. Apparently, by default the compiler reads it as *(matrix[i]), which is not what I was looking for.

Thank you again for the help.
Topic archived. No new replies allowed.