function to free char** for mem leaks

This commit is contained in:
Jack Halford 2016-11-10 12:08:01 +01:00
parent 995261de4e
commit c64cf9a5a4
13 changed files with 86 additions and 34 deletions

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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_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_stack(char **board, int size, int i, char c);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
int ft_lstsize(t_list *lst);
void ft_lst_cfree(void *ptr, size_t size);
int get_next_line(int const fd, char **line);
void ft_sstrfree(char **sstr);
#endif

View file

@ -6,24 +6,17 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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 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;
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, '*');
space -= blob_size % 4;
if (space < 0)
{
ft_board_remove(board, "^*");
return (0);
}
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);
space -= 4;
if (space < 0)
{
ft_board_remove(board, "^*");
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, "^*");
return (ft_solver(board, lttmn, space, size));
}

View file

@ -6,27 +6,27 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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_list *list;
int i;
int n;
n = blob_size;
i = y - 1;
i = -1;
while (++i < size * size)
{
if (board[i / size][i % size] == '*')
{
n--;
list = lttmn;
while (list)
{
@ -41,8 +41,6 @@ int ft_fit_blob(
ttmn->placed = 0;
}
}
if (n < 4)
return (0);
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
ft_lstiter(lttmn, &ft_ttmn_reset);
ft_solver(board, lttmn, size * size - 4 * g_ttmn, size);
ft_sstrfree(board);
size--;
}
ft_lstdel(&lttmn, &ft_lst_cfree);
ft_board_print(g_sol);
return (0);
}

View 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);
}

View 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++;
}
}