diff options
| author | Kirill Petrashin <kirill8201@yandex.ru> | 2026-04-03 17:59:33 +0300 |
|---|---|---|
| committer | Kirill Petrashin <kirill8201@yandex.ru> | 2026-04-03 17:59:33 +0300 |
| commit | 6ea83172b08b0e8fe1c0fd3a84d2bd5da954d6cc (patch) | |
| tree | c68aee3ae47ffb54365077c5a2b753b5bf43a2ed /path.c | |
| parent | 104d23d1e4949fe01038f4a2fd706acf163b55c9 (diff) | |
| download | astar-6ea83172b08b0e8fe1c0fd3a84d2bd5da954d6cc.tar.xz | |
Make the cost_so_far array dynamic to avoid SEGFAULTs
Diffstat (limited to 'path.c')
| -rw-r--r-- | path.c | 38 |
1 files changed, 34 insertions, 4 deletions
@@ -118,8 +118,7 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star PositionPQ *frontier = ppq_new(start, 0); visited_clear(visited, width, height); - size_t cost_so_far[height][width]; - memset(cost_so_far, 0xFF, sizeof(size_t) * height * width); + size_t **cost_so_far = cost_so_far_new(width, height); cost_so_far[start.y][start.x] = 0; while (frontier != NULL) { @@ -128,6 +127,7 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star if (cur.x == end.x && cur.y == end.y) { ppq_free(frontier); + cost_so_far_free(cost_so_far, height); return path; /* Found path */ } @@ -151,12 +151,16 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star if (anim(map, width, height, start, end, &cur, visited, frontier) == -1) { clear(); path_free(path, height); + ppq_free(frontier); + cost_so_far_free(cost_so_far, height); return NULL; } } } path_free(path, height); + ppq_free(frontier); + cost_so_far_free(cost_so_far, height); return NULL; } @@ -185,8 +189,7 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, PositionPQ *frontier = ppq_new(start, 0); visited_clear(visited, width, height); - size_t cost_so_far[height][width]; - memset(cost_so_far, 0xFF, sizeof(size_t) * height * width); + size_t **cost_so_far = cost_so_far_new(width, height); cost_so_far[start.y][start.x] = 0; while (frontier != NULL) { @@ -195,6 +198,7 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, if (cur.x == end.x && cur.y == end.y) { ppq_free(frontier); + cost_so_far_free(cost_so_far, height); return path; /* Found path */ } @@ -219,12 +223,16 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, if (anim(map, width, height, start, end, &cur, visited, frontier) == -1) { clear(); path_free(path, height); + ppq_free(frontier); + cost_so_far_free(cost_so_far, height); return NULL; } } } path_free(path, height); + ppq_free(frontier); + cost_so_far_free(cost_so_far, height); return NULL; } @@ -313,3 +321,25 @@ void visited_free(char **visited, size_t height) { free(visited); return; } + +size_t **cost_so_far_new(size_t width, size_t height) { + size_t **cost_so_far = malloc(sizeof(size_t*) * height); + if (cost_so_far == NULL) return NULL; + + for (size_t row = 0; row < height; row++) { + cost_so_far[row] = malloc(sizeof(size_t) * width); + if (cost_so_far[row] == NULL) return NULL; + memset(cost_so_far[row], 0xFF, width*sizeof(size_t)); + } + + return cost_so_far; +} + +void cost_so_far_free(size_t **cost_so_far, size_t height) { + if (cost_so_far == NULL) return; + for (size_t row = 0; row < height; row++) { + free(cost_so_far[row]); + } + free(cost_so_far); + return; +} |
