typedef struct Cell {
float altitude;
int type;
}Cell;
void MAZE(FILE *fp, Cell *Map);
int main(void) {
FILE *fp = fopen("map.bin", "rb");
Cell *Map;
Map = read_file(fp);
char choice;
while (1) {
system("color 0f");
system("cls");
puts("Main menu:");
puts("1. Show map by type.");
puts("2. Show map by altitude.");
puts("3. Build route.");
puts("4. Find suitable places for biker jumps.");
puts("5. Quit.");
std::cin >> choice;
std::cin.ignore();
switch (choice) {
case '1': {
display_map_by_type(fp, Map);
continue;
}
case '2': {
display_map_by_altitude(fp, Map);
continue;
}
case '3': {
MAZE(fp, Map);
continue;
}
case '5': {
puts("You've decided to quit.");
free(Map);
return 0;
}
default: {
system("color 9f");
puts("Invalid choice.");
puts(Press);
getche();
continue;
}
}
return 0;
}
}
void MAZE(FILE *fp, Cell *Map) {
bool (*initial_maze)[10][10] = (bool (*)[10][10])malloc(sizeof(bool[10][10]));
if (initial_maze == NULL) {
fprintf(stderr, NaM);
exit(-3);
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (Map[i * 10 + j].type == 2 || Map[i * 10 + j].type == 4) {
*initial_maze[i][j] = false;
}
else {
*initial_maze[i][j] = true;
}
}
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
printf("[%d]", Map[i *10 + j].type);
}
putchar('\n');
}
putchar('\n');
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++) {
printf("%3s", *initial_maze[i][j] ? "." : "[]");
}
putchar('\n');
}
getchar();
free(initial_maze);
}
This program has an array of objects loaded into the heap - Cell *Map
. The array is available in the main
and is passable and free()
-able in the main
or any other function. Yet for some reason there's a segfault when choosing option 3. Why the segfault occurs (both with Cell *Map
and bool *initial_maze[10][10]
) is unknown to me. At least I don't find any reason for a segfault, no uninitialized memory is accessed, no multiple free()
-s. Technically there is a free()
in the switch
which is in a loop, but that still can't be it, because right after that there is a return
statement. To top it off it doesn't always crash either, although it does in most cases.
The debuggers in Visual Studio and Code Blocks pointed at the free(initial_maze);
line, but there's nothing wring with it, there's nothing that could have invalidated the array before that.
For some odd reason Visual Studio also pointed at the system("color 0f");
line as if it could trigger an exception.
As for some context, what's happening in the void MAZE(FILE *fp, Cell *Map);
function. It's supposed to solve a maze, but first it has to create a 2d bool array based on the 1d array of objects passed to it.
I've read the documentation, I'm familiar with the rules when using dynamical memory.
If you want some output:
[0][0][2][3][1][4][1][2][0][3]
[2][1][1][0][3][4][0][2][1][0]
[4][0][4][3][1][1][0][4][1][2]
[2][1][3][1][3][2][1][3][1][0]
[2][2][3][0][0][1][2][4][4][3]
[1][3][2][1][2][2][0][1][1][3]
[4][4][0][3][1][2][0][4][1][2]
[3][0][4][4][4][1][0][3][2][0]
[1][4][3][4][3][4][0][1][1][0]
[1][1][2][2][1][1][3][1][3][3]
. . [] . . [] . [] . .
[] . . . . [] . [] . .
[] . [] . . . . [] . []
[] . . . . [] . . . .
[] [] . . . . [] [] [] .
. . [] . [] [] . . . .
[] [] . . . [] . [] . []
. . [] [] [] . . . [] .
. [] . [] . [] . . . .
. . [] [] . . . . . .
and then the program hangs for a bit and crashes.