nissy-classic

Stable branch of nissy
git clone https://git.tronto.net/nissy-classic
Download | Log | Files | Refs | README | LICENSE

commit 9c78331ef2ebe2f1dcef886b0043431469bc1a29
parent 7c1cffb3f7c2be0d9ccc954a00b00db3bf4e2899
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Sun, 17 Sep 2023 23:38:46 +0200

Fixed various memory leaks

Diffstat:
Msrc/moves.c | 3++-
Msrc/solve.c | 5++++-
Msrc/utils.c | 15+++++----------
3 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/src/moves.c b/src/moves.c @@ -481,7 +481,7 @@ init_moves(void) } if (read_mtables_file()) - return; + goto init_moves_end; fprintf(stderr, "Cannot load %s, generating it\n", "mtables"); @@ -541,6 +541,7 @@ init_moves(void) if (!write_mtables_file()) fprintf(stderr, "Error writing mtables\n"); +init_moves_end: for (i = 0; i < NMOVES; i++) free_alg(equiv_alg[i]); } diff --git a/src/solve.c b/src/solve.c @@ -390,6 +390,7 @@ solve(Cube cube, Step *step, SolveOptions *opts) { bool ready; int i, d, op, nt; + Alg *algaux; AlgList *sols; Cube c; Trans tt[NTRANS]; @@ -417,7 +418,9 @@ solve(Cube cube, Step *step, SolveOptions *opts) for (i = 0; i < nt; i++) { c = apply_trans(tt[i], cube); if (step->is_done(c)) { - append_alg(sols, new_alg("")); + algaux = new_alg(""); + append_alg(sols, algaux); + free_alg(algaux); return sols; } } diff --git a/src/utils.c b/src/utils.c @@ -3,8 +3,8 @@ void apply_permutation(int *perm, int *set, int n) { - int *aux = malloc(n * sizeof(int)); int i; + int aux[n]; if (!is_perm(perm, n)) return; @@ -13,7 +13,6 @@ apply_permutation(int *perm, int *set, int n) aux[i] = set[perm[i]]; memcpy(set, aux, n * sizeof(int)); - free(aux); } int @@ -53,8 +52,8 @@ factorial(int n) void index_to_perm(int p, int n, int *r) { - int *a = malloc(n * sizeof(int)); int i, j, c; + int a[n]; for (i = 0; i < n; i++) a[i] = 0; @@ -72,8 +71,6 @@ index_to_perm(int p, int n, int *r) a[j-1] = 1; p %= factorial(n-i-1); } - - free(a); } void @@ -143,22 +140,22 @@ int_to_sum_zero_array(int x, int b, int n, int *a) int invert_digits(int a, int b, int n) { - int i, ret, *r = malloc(n * sizeof(int)); + int i, ret; + int r[n]; int_to_digit_array(a, b, n, r); for (i = 0; i < n; i++) r[i] = (b-r[i]) % b; ret = digit_array_to_int(r, n, b); - free(r); return ret; } bool is_perm(int *a, int n) { - int *aux = malloc(n * sizeof(int)); int i; + int aux[n]; for (i = 0; i < n; i++) aux[i] = 0; @@ -174,8 +171,6 @@ is_perm(int *a, int n) if (!aux[i]) return false; - free(aux); - return true; }