field *field_pointer;
/* Enumerates the whole field by saving the coordinates of the free pieces and the dimensions */
void enum_field(char *filename) {
FILE *maze;
maze = fopen(filename, "r");
int i = 0, j = 0, chr;
field_pointer = (field *)malloc(sizeof(field *));
field_pointer->empty_coords = (int **)malloc(sizeof(int *));
field_pointer->free_spaces = 0;
while ((chr = fgetc(maze)) != EOF) {
if (chr == '\n') {
i = 0;
j++;
} else {
if (chr != '#') {
field_pointer->empty_coords[field_pointer->free_spaces] = (int *)malloc(2 * sizeof(int));
field_pointer->empty_coords[field_pointer->free_spaces][0] = i;
field_pointer->empty_coords[field_pointer->free_spaces][1] = j;
field_pointer->free_spaces++;
// I believe the error is here V
field_pointer->empty_coords = (int **)realloc(field_pointer->empty_coords,
(field_pointer->free_spaces + 1) * sizeof(int *));
if (chr == 'E') {
field_pointer->E[0] = i;
field_pointer->E[1] = j;
} else if (chr == 'S') {
field_pointer->S[0] = i;
field_pointer->S[1] = j;
}
}
i++;
}
}
field_pointer->x = i;
field_pointer->y = j;
fclose(maze);
}
Well, so I have this C function here, which helps me enumerate a 2D ASCII maze so I can then turn it into a graph.
The ASCII maze is denoted by # as walls, free spaces as walkable road and S as Start and E as Exit.
The function doesn't work and throws a 0xC0000374 which probably means corrupted heap.
I tried to investigate further and noticed that most likely,
the realloc function which is supposed to enlarge the two-dimensional array is at fault.
I am relatively new to C, and have been staring a lot at my code, can you help me with another POV and fresh eyes as to what I might be doing wrong? Here is the layout of the field struct as well:
typedef struct field {
int x, y, free_spaces;
int **empty_coords;
int S[2];
int E[2];
} field;