Your code is declaring an array of pointers to pointer (three-asterisk array) while the assignment places a pointer to a single block of size cells_x * cells_y * cells_z
into it. This is incorrect, because the users of your extern
variable expect a "jagged" array (i.e. an array of pointers to pointer):
static double*** initGrid(size_t cells_x, size_t cells_y, size_t cells_z) {
double ***grid;
grid = malloc(cells_x, sizeof(double**));
for (size_t x = 0 ; x != cells_x ; x++) {
grid[x] = malloc(cells_y, sizeof(double*));
for (size_t y = 0 ; y != cells_y ; y++) {
grid[x][y] = calloc(cells_z, sizeof(double));
}
}
return grid;
}
static void freeGrid(double ***grid, size_t cells_x, size_t cells_y) {
for (size_t x = 0 ; x != cells_x ; x++) {
for (size_t y = 0 ; y != cells_y ; y++) {
free(grid[x][y]);
}
free(grid[x]);
}
free(grid);
}
You need to allocate and free jagged array in a different way - with multiple loops, or declare your grid
as a pointer to an array of arrays.