function to free char** for mem leaks
This commit is contained in:
parent
995261de4e
commit
c64cf9a5a4
13 changed files with 86 additions and 34 deletions
BIN
fillit/fillit
BIN
fillit/fillit
Binary file not shown.
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 17:38:16 by jhalford #+# #+# */
|
/* Created: 2016/11/03 17:38:16 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/04 14:07:38 by jhalford ### ########.fr */
|
/* Updated: 2016/11/08 16:51:11 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -47,7 +47,7 @@ int ft_solved(char **board);
|
||||||
|
|
||||||
int ft_check_waste(char **board, t_list *lttmn, int space, int size);
|
int ft_check_waste(char **board, t_list *lttmn, int space, int size);
|
||||||
int ft_fit_blob(
|
int ft_fit_blob(
|
||||||
char **board, t_list *lttmn, int space, int size, int blob_size, int i);
|
char **board, t_list *lttmn, int space, int size);
|
||||||
int ft_floodfill_recursive(char **board, int size, int i, char c);
|
int ft_floodfill_recursive(char **board, int size, int i, char c);
|
||||||
int ft_floodfill_stack(char **board, int size, int i, char c);
|
int ft_floodfill_stack(char **board, int size, int i, char c);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 15:00:29 by jhalford #+# #+# */
|
/* Created: 2016/11/03 15:00:29 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/04 13:35:02 by jhalford ### ########.fr */
|
/* Updated: 2016/11/08 17:23:41 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -96,5 +96,8 @@ t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
|
||||||
|
|
||||||
void ft_lsteadd(t_list **alst, t_list *new);
|
void ft_lsteadd(t_list **alst, t_list *new);
|
||||||
int ft_lstsize(t_list *lst);
|
int ft_lstsize(t_list *lst);
|
||||||
|
void ft_lst_cfree(void *ptr, size_t size);
|
||||||
int get_next_line(int const fd, char **line);
|
int get_next_line(int const fd, char **line);
|
||||||
|
|
||||||
|
void ft_sstrfree(char **sstr);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,24 +6,17 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/04 13:24:35 by jhalford #+# #+# */
|
/* Created: 2016/11/04 13:24:35 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/04 13:30:16 by jhalford ### ########.fr */
|
/* Updated: 2016/11/08 17:27:04 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "fillit.h"
|
#include "fillit.h"
|
||||||
|
|
||||||
int ft_check_waste(char **board, t_list *lttmn, int space, int size)
|
static int ft_waste_loop(char **board, t_list *lttmn, int space, int size)
|
||||||
{
|
{
|
||||||
t_ttmn *ttmn;
|
|
||||||
int i;
|
|
||||||
int blob_size;
|
int blob_size;
|
||||||
|
int i;
|
||||||
|
|
||||||
if (!lttmn)
|
|
||||||
return (ft_solved(board));
|
|
||||||
ttmn = (t_ttmn *)lttmn->content;
|
|
||||||
if (ttmn->placed)
|
|
||||||
return (ft_solver(board, lttmn->next, space, size));
|
|
||||||
space = size * size - 4 * g_ttmn;
|
|
||||||
i = -1;
|
i = -1;
|
||||||
while (++i < size * size)
|
while (++i < size * size)
|
||||||
{
|
{
|
||||||
|
|
@ -33,22 +26,33 @@ int ft_check_waste(char **board, t_list *lttmn, int space, int size)
|
||||||
blob_size = ft_floodfill_recursive(board, size, i, '*');
|
blob_size = ft_floodfill_recursive(board, size, i, '*');
|
||||||
space -= blob_size % 4;
|
space -= blob_size % 4;
|
||||||
if (space < 0)
|
if (space < 0)
|
||||||
{
|
|
||||||
ft_board_remove(board, "^*");
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
|
||||||
if (blob_size / 4 == 1)
|
if (blob_size / 4 == 1)
|
||||||
{
|
{
|
||||||
if (ft_fit_blob(board, lttmn, space, size, blob_size, i))
|
if (ft_fit_blob(board, lttmn, space, size))
|
||||||
return (1);
|
return (1);
|
||||||
space -= 4;
|
space -= 4;
|
||||||
if (space < 0)
|
if (space < 0)
|
||||||
{
|
|
||||||
ft_board_remove(board, "^*");
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_check_waste(char **board, t_list *lttmn, int space, int size)
|
||||||
|
{
|
||||||
|
int loop_ret;
|
||||||
|
|
||||||
|
if (!lttmn)
|
||||||
|
return (ft_solved(board));
|
||||||
|
if (((t_ttmn*)lttmn->content)->placed)
|
||||||
|
return (ft_solver(board, lttmn->next, space, size));
|
||||||
|
if ((loop_ret = ft_waste_loop(board, lttmn, space, size)) < 2)
|
||||||
|
{
|
||||||
|
if (loop_ret == 0)
|
||||||
|
ft_board_remove(board, "^*");
|
||||||
|
return (loop_ret);
|
||||||
|
}
|
||||||
ft_board_remove(board, "^*");
|
ft_board_remove(board, "^*");
|
||||||
return (ft_solver(board, lttmn, space, size));
|
return (ft_solver(board, lttmn, space, size));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,27 +6,27 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 17:39:49 by jhalford #+# #+# */
|
/* Created: 2016/11/03 17:39:49 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/03 17:42:03 by jhalford ### ########.fr */
|
/* Updated: 2016/11/08 16:59:50 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "fillit.h"
|
#include "fillit.h"
|
||||||
|
|
||||||
int ft_fit_blob(
|
int ft_fit_blob(
|
||||||
char **board, t_list *lttmn, int space, int size, int blob_size, int y)
|
char **board,
|
||||||
|
t_list *lttmn,
|
||||||
|
int space,
|
||||||
|
int size)
|
||||||
{
|
{
|
||||||
t_ttmn *ttmn;
|
t_ttmn *ttmn;
|
||||||
t_list *list;
|
t_list *list;
|
||||||
int i;
|
int i;
|
||||||
int n;
|
|
||||||
|
|
||||||
n = blob_size;
|
i = -1;
|
||||||
i = y - 1;
|
|
||||||
while (++i < size * size)
|
while (++i < size * size)
|
||||||
{
|
{
|
||||||
if (board[i / size][i % size] == '*')
|
if (board[i / size][i % size] == '*')
|
||||||
{
|
{
|
||||||
n--;
|
|
||||||
list = lttmn;
|
list = lttmn;
|
||||||
while (list)
|
while (list)
|
||||||
{
|
{
|
||||||
|
|
@ -41,8 +41,6 @@ int ft_fit_blob(
|
||||||
ttmn->placed = 0;
|
ttmn->placed = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (n < 4)
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 17:29:51 by jhalford #+# #+# */
|
/* Created: 2016/11/03 17:29:51 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/04 13:48:55 by jhalford ### ########.fr */
|
/* Updated: 2016/11/07 12:16:01 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 17:36:27 by jhalford #+# #+# */
|
/* Created: 2016/11/03 17:36:27 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/04 13:49:41 by jhalford ### ########.fr */
|
/* Updated: 2016/11/08 17:26:58 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 17:34:39 by jhalford #+# #+# */
|
/* Created: 2016/11/03 17:34:39 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/04 13:25:08 by jhalford ### ########.fr */
|
/* Updated: 2016/11/08 17:26:20 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 17:31:46 by jhalford #+# #+# */
|
/* Created: 2016/11/03 17:31:46 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/04 13:38:18 by jhalford ### ########.fr */
|
/* Updated: 2016/11/08 16:49:44 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 17:32:01 by jhalford #+# #+# */
|
/* Created: 2016/11/03 17:32:01 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/03 17:34:16 by jhalford ### ########.fr */
|
/* Updated: 2016/11/07 12:14:50 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 17:31:13 by jhalford #+# #+# */
|
/* Created: 2016/11/03 17:31:13 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/04 13:48:23 by jhalford ### ########.fr */
|
/* Updated: 2016/11/08 17:26:50 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -36,8 +36,10 @@ int main(int ac, char **av)
|
||||||
board = ft_board_init(size);
|
board = ft_board_init(size);
|
||||||
ft_lstiter(lttmn, &ft_ttmn_reset);
|
ft_lstiter(lttmn, &ft_ttmn_reset);
|
||||||
ft_solver(board, lttmn, size * size - 4 * g_ttmn, size);
|
ft_solver(board, lttmn, size * size - 4 * g_ttmn, size);
|
||||||
|
ft_sstrfree(board);
|
||||||
size--;
|
size--;
|
||||||
}
|
}
|
||||||
|
ft_lstdel(<tmn, &ft_lst_cfree);
|
||||||
ft_board_print(g_sol);
|
ft_board_print(g_sol);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
fillit/src/libft/ft_lst_cfree.c
Normal file
20
fillit/src/libft/ft_lst_cfree.c
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lst_cfree.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2016/11/04 11:09:10 by jhalford #+# #+# */
|
||||||
|
/* Updated: 2016/11/08 17:23:34 by jhalford ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_lst_cfree(void *ptr, size_t size)
|
||||||
|
{
|
||||||
|
(void)size;
|
||||||
|
if (ptr)
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
25
fillit/src/libft/ft_sstrfree.c
Normal file
25
fillit/src/libft/ft_sstrfree.c
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_sstrfree.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2016/11/08 17:01:24 by jhalford #+# #+# */
|
||||||
|
/* Updated: 2016/11/08 17:09:26 by jhalford ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_sstrfree(char **sstr)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (sstr[i])
|
||||||
|
{
|
||||||
|
ft_strdel(sstr + i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue