From 6ea83172b08b0e8fe1c0fd3a84d2bd5da954d6cc Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Fri, 3 Apr 2026 17:59:33 +0300 Subject: Make the cost_so_far array dynamic to avoid SEGFAULTs --- path.c | 38 ++++++++++++++++++++++++++++++++++---- path.h | 4 ++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/path.c b/path.c index 589edd6..2b07c55 100644 --- a/path.c +++ b/path.c @@ -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; +} diff --git a/path.h b/path.h index 12cfa80..002690d 100644 --- a/path.h +++ b/path.h @@ -21,4 +21,8 @@ char **visited_new(size_t width, size_t height); void visited_clear(char **visited, size_t width, size_t height); void visited_free(char **visited, size_t height); +/* Helper funcs for the cost_so_far array */ +size_t **cost_so_far_new(size_t width, size_t height); +void cost_so_far_free(size_t **cost_so_far, size_t height); + #endif /* ASTAR_H_ */ -- cgit v1.2.3