Memory leak is fixed

This commit is contained in:
Kolomiets Yaroslav 2016-12-22 15:36:57 +02:00
parent 12fc9b3108
commit 46387d1666
3 changed files with 46 additions and 59 deletions

View file

@ -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); 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); 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 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); void cover(t_node *to_cover);

View file

@ -148,8 +148,8 @@ void add_rows(int *types, int amount, int size, t_node **cols_arr)
void cover(t_node *to_cover) void cover(t_node *to_cover)
{ {
t_node *step_vert; static t_node *step_vert;
t_node *step_horiz; static t_node *step_horiz;
to_cover->left->right = to_cover->right; to_cover->left->right = to_cover->right;
to_cover->right->left = to_cover->left; to_cover->right->left = to_cover->left;
@ -168,8 +168,8 @@ void cover(t_node *to_cover)
void uncover(t_node *to_uncover) void uncover(t_node *to_uncover)
{ {
t_node *step_vert; static t_node *step_vert;
t_node *step_horiz; static t_node *step_horiz;
step_vert = to_uncover->up; step_vert = to_uncover->up;
while (step_vert != to_uncover) 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; char *map;
int i;
int map_size; int map_size;
int i;
map_size = root->size * root->size + 1; map_size = size * size + 1;
map = malloc(sizeof(char) * map_size); 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; map[map_size] = 0;
while (sol)
{
fill_map(map, root->size, (*(t_node **)sol->content), amount);
sol = sol->next;
amount--;
}
i = 0; i = 0;
while (i < root->size * root->size) 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') if (map[i] >= 'A' && map[i] <= 'Z')
ft_putchar(map[i]); ft_putchar(map[i]);
else else
ft_putchar('.'); ft_putchar('.');
if ((i + 1) % root->size == 0) if ((i + 1) % size == 0)
ft_putchar('\n'); ft_putchar('\n');
i++; i++;
} }
exit(0); 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 *current_col;
t_node *row; t_node *row;
t_node *j; t_node *j;
if (k == amount) if (k == amount)
print_solution(solution, root, amount); print_solution(solution, amount, root->size);
if (root->right == root)
return ;
current_col = root->right; current_col = root->right;
cover(current_col); cover(current_col);
row = current_col->down; row = current_col->down;
while (row != current_col) while (row != current_col)
{ {
ft_lstadd(&solution, ft_lstnew(&row, sizeof(t_node *))); solution[k] = row;
j = row->right; j = row->right;
while (j != row) while (j != row)
{ {
@ -263,8 +250,6 @@ void search(t_node* root, t_list *solution, int k, int amount)
j = j->right; j = j->right;
} }
search(root, solution, k + 1, amount); search(root, solution, k + 1, amount);
row = *((t_node**)(ft_lstpop(&solution)->content));
current_col = row->column;
j = row->left; j = row->left;
while (j != row) while (j != row)
{ {

View file

@ -50,7 +50,7 @@ int main(int argc, char **argv)
int size; int size;
t_node *r; t_node *r;
t_node **cols_arr; t_node **cols_arr;
t_list *ans; t_node **ans;
ans = NULL; ans = NULL;
i = 0; i = 0;
@ -83,8 +83,10 @@ int main(int argc, char **argv)
types = get_types(ttr); types = get_types(ttr);
amount = get_amount(ttr); amount = get_amount(ttr);
ans = (t_node**)malloc(sizeof(t_node*) * amount);
size = ft_sqrt_ceil(amount * 4); size = ft_sqrt_ceil(amount * 4);
while (1) { while (1)
{
r = init_root(size); r = init_root(size);
cols_arr = add_cols(r, (size * size + amount)); cols_arr = add_cols(r, (size * size + amount));
add_rows(types, amount, size, cols_arr); add_rows(types, amount, size, cols_arr);