Fix memory safety errors in examples.
This commit is contained in:
1 parent
c55bb6b9ad
commit
b3a0d70147
2 files changed
+9
-6
No files matched your search
+4
-4
@@ -61,11 +61,11 @@ static void astar(array_point_t path, const string_t maze, int width)
|
|||||||
for (size_t i = 0; i < 8; i++) {
|
for (size_t i = 0; i < 8; i++) {
|
||||||
point_emplace(next, current->x + deltas[i].x, current->y + deltas[i].y, 0);
|
point_emplace(next, current->x + deltas[i].x, current->y + deltas[i].y, 0);
|
||||||
if (string_get_char(maze, point_index(next, width)) != '#') {
|
if (string_get_char(maze, point_index(next, width)) != '#') {
|
||||||
int *new_cost = cost_point_get(costs, current);
|
int new_cost = *cost_point_get(costs, current);
|
||||||
int *cost = cost_point_get(costs, next);
|
int *cost = cost_point_get(costs, next);
|
||||||
if (cost == NULL || *new_cost < *cost) {
|
if (cost == NULL || new_cost < *cost) {
|
||||||
cost_point_set_at(costs, next, *new_cost);
|
cost_point_set_at(costs, next, new_cost);
|
||||||
next->priority = *new_cost + abs(goal->x - next->x) + abs(goal->y - next->y);
|
next->priority = new_cost + abs(goal->x - next->x) + abs(goal->y - next->y);
|
||||||
prio_point_push(front, next);
|
prio_point_push(front, next);
|
||||||
step_point_set_at(from, next, current);
|
step_point_set_at(from, next, current);
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-2
@@ -208,11 +208,14 @@ dict_read_from_file(dict_mph_t dict, array_string_t arr, const char filename[])
|
|||||||
// Not clean as items of 'dict' points are linked to items of 'arr'
|
// Not clean as items of 'dict' points are linked to items of 'arr'
|
||||||
// and the containers are not aware of this link.
|
// and the containers are not aware of this link.
|
||||||
array_string_push_back (arr, str);
|
array_string_push_back (arr, str);
|
||||||
dict_mph_set_at(dict, string_get_cstr(*array_string_back(arr)), line);
|
|
||||||
line++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Now 'arr' won't change anymore and we can get reliable pointers from it.
|
||||||
|
for M_EACH(item, arr, array_string_t) {
|
||||||
|
dict_mph_set_at(dict, string_get_cstr(*item), line);
|
||||||
|
line++;
|
||||||
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user