From 46387d1666d7c74c18b2f3b9072d0e72d6b83d1b Mon Sep 17 00:00:00 2001 From: Kolomiets Yaroslav Date: Thu, 22 Dec 2016 15:36:57 +0200 Subject: [PATCH] Memory leak is fixed --- inc/fillit.h | 4 +-- src/dancing_links.c | 83 +++++++++++++++++++-------------------------- src/main.c | 18 +++++----- 3 files changed, 46 insertions(+), 59 deletions(-) diff --git a/inc/fillit.h b/inc/fillit.h index c6d3ce9..273b06d 100644 --- a/inc/fillit.h +++ b/inc/fillit.h @@ -87,7 +87,7 @@ t_node *add_node_with_coord(t_node *col, t_coord row, int type); t_node **add_cols(t_node *root, int number); -void print_solution(t_list *sol, t_node *root, int amount); +void print_solution(t_node **sol, int amount, int size); void add_rows(int *types, int amount, int size, t_node **cols_arr); @@ -95,7 +95,7 @@ void link_row(int *col_nums, t_node **cols_arr); void add_row(int *col_numbers, t_node **cols_arr, t_coord coord, int type); -void search(t_node* root, t_list *solution, int k, int amount); +void search(t_node* root, t_node **solution, int k, int amount); void cover(t_node *to_cover); diff --git a/src/dancing_links.c b/src/dancing_links.c index fbb962e..f2609fd 100644 --- a/src/dancing_links.c +++ b/src/dancing_links.c @@ -148,8 +148,8 @@ void add_rows(int *types, int amount, int size, t_node **cols_arr) void cover(t_node *to_cover) { - t_node *step_vert; - t_node *step_horiz; + static t_node *step_vert; + static t_node *step_horiz; to_cover->left->right = to_cover->right; to_cover->right->left = to_cover->left; @@ -168,8 +168,8 @@ void cover(t_node *to_cover) void uncover(t_node *to_uncover) { - t_node *step_vert; - t_node *step_horiz; + static t_node *step_vert; + static t_node *step_horiz; step_vert = to_uncover->up; while (step_vert != to_uncover) @@ -203,59 +203,46 @@ void fill_map(char *map, int size, t_node *ttr, int letter) } } -void print_solution(t_list *sol, t_node *root, int amount) +void print_solution(t_node **solution, int amount, int size) { - // doesn't work for one ttr - // if ttr has dimensions 3x2 - // output square must be 3x3 - // but func print 4x4 + char *map; + int map_size; + int i; - char *map; - int i; - int map_size; - - map_size = root->size * root->size + 1; - map = malloc(sizeof(char) * map_size); - // init with zeroes by ft_memalloc - // or in some other way. - // map can contain garbage - map[map_size] = 0; - while (sol) - { - fill_map(map, root->size, (*(t_node **)sol->content), amount); - sol = sol->next; - amount--; - } - i = 0; - while (i < root->size * root->size) - { - if (map[i] >= 'A' && map[i] <= 'Z') - ft_putchar(map[i]); - else - ft_putchar('.'); - if ((i + 1) % root->size == 0) - ft_putchar('\n'); - i++; - } - exit(0); + map_size = size * size + 1; + map = malloc(sizeof(char) * map_size); + map[map_size] = 0; + i = 0; + while (i++ < amount) + fill_map(map, size, solution[i - 1], i); + i = 0; + while (i < map_size - 1) + { + if (map[i] >= 'A' && map[i] <= 'Z') + ft_putchar(map[i]); + else + ft_putchar('.'); + if ((i + 1) % size == 0) + ft_putchar('\n'); + i++; + } + exit(0); } -void search(t_node* root, t_list *solution, int k, int amount) +void search(t_node* root,t_node **solution, int k, int amount) { - t_node *current_col; - t_node *row; - t_node *j; + t_node *current_col; + t_node *row; + t_node *j; - if (k == amount) - print_solution(solution, root, amount); - if (root->right == root) - return ; + if (k == amount) + print_solution(solution, amount, root->size); current_col = root->right; cover(current_col); row = current_col->down; while (row != current_col) { - ft_lstadd(&solution, ft_lstnew(&row, sizeof(t_node *))); + solution[k] = row; j = row->right; while (j != row) { @@ -263,8 +250,6 @@ void search(t_node* root, t_list *solution, int k, int amount) j = j->right; } search(root, solution, k + 1, amount); - row = *((t_node**)(ft_lstpop(&solution)->content)); - current_col = row->column; j = row->left; while (j != row) { @@ -275,4 +260,4 @@ void search(t_node* root, t_list *solution, int k, int amount) } uncover(current_col); return ; -} \ No newline at end of file +} diff --git a/src/main.c b/src/main.c index 76e9024..976f37c 100644 --- a/src/main.c +++ b/src/main.c @@ -50,7 +50,7 @@ int main(int argc, char **argv) int size; t_node *r; t_node **cols_arr; - t_list *ans; + t_node **ans; ans = NULL; i = 0; @@ -83,14 +83,16 @@ int main(int argc, char **argv) types = get_types(ttr); amount = get_amount(ttr); + ans = (t_node**)malloc(sizeof(t_node*) * amount); size = ft_sqrt_ceil(amount * 4); - while (1) { - r = init_root(size); - cols_arr = add_cols(r, (size * size + amount)); - add_rows(types, amount, size, cols_arr); - search(r, ans, 0, amount); - size++; - } + while (1) + { + r = init_root(size); + cols_arr = add_cols(r, (size * size + amount)); + add_rows(types, amount, size, cols_arr); + search(r, ans, 0 ,amount); + size++; + } //print_ans(ans); //printf("%s\n %d, %d", strct_ttr[0]->t, strct_ttr[0]->x, strct_ttr[0]->y); //to_letters(ttr);