fixing the leak valgrind and conditionnal jump on some functions cause i forget to initialize the var to NULL i just need to remember this

This commit is contained in:
Maoake
2025-12-23 09:21:17 +00:00
parent 96a0a6d8f0
commit 85b83f3cc7
9 changed files with 32 additions and 41 deletions

View File

@@ -14,10 +14,5 @@
void bucket_algo(void) void bucket_algo(void)
{ {
/*int range;
t_tab *preset;
range = range_bucket(piles->a);
preset = get_tabs(piles->a, range);*/
return ; return ;
} }

View File

@@ -34,8 +34,7 @@ int get_next_lower(t_stack *first, int old_lower)
int next_lower; int next_lower;
tmp = first; tmp = first;
next_lower = 2147483646; next_lower = 2147483647;
while (tmp->next != first) while (tmp->next != first)
{ {
if (old_lower < tmp->value && tmp->value <= next_lower) if (old_lower < tmp->value && tmp->value <= next_lower)

View File

@@ -20,13 +20,12 @@ t_tab *allocate_tab(int range_max, int nb)
tab = malloc(sizeof(t_tab)); tab = malloc(sizeof(t_tab));
if (!tab) if (!tab)
return (NULL); return (NULL);
tab->next = NULL;
tab->max_range = range_max; tab->max_range = range_max;
tab->nb_in = nb; tab->nb_in = nb;
return (tab); return (tab);
} }
#include <stdio.h>
t_tab *get_tabs(t_stack *first, int range) t_tab *get_tabs(t_stack *first, int range)
{ {
t_tab *tmp; t_tab *tmp;
@@ -44,7 +43,7 @@ t_tab *get_tabs(t_stack *first, int range)
{ {
tmp->next = get_next_tab(first, tmp, range); tmp->next = get_next_tab(first, tmp, range);
if (!(tmp->next)) if (!(tmp->next))
return (free_tab(first_tab)); return (free_tab(&first_tab));
tmp = tmp->next; tmp = tmp->next;
scan_nb_in_tab += tmp->nb_in; scan_nb_in_tab += tmp->nb_in;
} }
@@ -78,15 +77,12 @@ t_tab *get_next_tab(t_stack *first, t_tab *tab, int range)
return (next_tab); return (next_tab);
} }
t_tab *free_tab(t_tab *first) t_tab *free_tab(t_tab **first)
{ {
t_tab *tmp; if (!(*first))
return (NULL);
while (first) if ((*first)->next)
{ free_tab(&(*first)->next);
tmp = first->next; free(*first);
free(first);
first = tmp;
}
return (NULL); return (NULL);
} }

13
main.c
View File

@@ -50,14 +50,14 @@ int verif_no_double(int *tab, int len, int value)
int adding_number(int *tab, int len) int adding_number(int *tab, int len)
{ {
int stock; int stock;
stock = tab[0]; stock = tab[0];
while (!verif_no_double(tab, len, stock)) while (!verif_no_double(tab, len, stock))
{ {
stock = 0 + rand() % (250 - 0 + 1); stock = 0 + rand() % (250 - 0 + 1);
} }
return stock; return (stock);
} }
int *auto_shuffle(int len_tab) int *auto_shuffle(int len_tab)
@@ -72,21 +72,20 @@ int *auto_shuffle(int len_tab)
tab[0] = 0 + rand() % (250 - 0 + 1); tab[0] = 0 + rand() % (250 - 0 + 1);
while (i < len_tab) while (i < len_tab)
{ {
tab[i] = adding_number(tab, i); tab[i] = adding_number(tab, i - 1);
i++; i++;
} }
return (tab); return (tab);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc > 1) if (argc > 1)
{ {
if (strcmp(argv[1], "-t1") == 0) if (strcmp(argv[1], "-t2") == 0)
test1(argc, argv++);
else if (strcmp(argv[1], "-t2") == 0)
test2(argv++); test2(argv++);
else
test1(argc, argv);
} }
return (0); return (0);
} }

View File

@@ -45,6 +45,8 @@ t_stacks *init_big_stacks(int argc, char **argv)
t_stack *a; t_stack *a;
stacks = malloc(sizeof(t_stacks)); stacks = malloc(sizeof(t_stacks));
stacks->a = NULL;
stacks->b = NULL;
if (!stacks) if (!stacks)
return (NULL); return (NULL);
a = parsing(argc, argv); a = parsing(argc, argv);

View File

@@ -59,6 +59,8 @@ t_stacks *init_big_stacks2(int *tab, int len)
t_stack *a; t_stack *a;
stacks = malloc(sizeof(t_stacks)); stacks = malloc(sizeof(t_stacks));
stacks->a = NULL;
stacks->b = NULL;
if (!stacks) if (!stacks)
return (NULL); return (NULL);
a = parsing2(tab, len); a = parsing2(tab, len);

BIN
push_swap

Binary file not shown.

View File

@@ -68,7 +68,7 @@ int my_sqrt(int nb);
int get_max_number(t_stack *first); int get_max_number(t_stack *first);
int get_min_number(t_stack *first); int get_min_number(t_stack *first);
int range_bucket(t_stack *first); int range_bucket(t_stack *first);
t_tab *free_tab(t_tab *first); t_tab *free_tab(t_tab **first);
t_tab *get_next_tab(t_stack *first, t_tab *tab, int range); t_tab *get_next_tab(t_stack *first, t_tab *tab, int range);
t_tab *init_first_tab(t_stack *first, int range); t_tab *init_first_tab(t_stack *first, int range);
t_tab *allocate_tab(int range_max, int nb); t_tab *allocate_tab(int range_max, int nb);

View File

@@ -14,7 +14,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int test1(int argc, char **argv) int test1(int argc, char **argv)
{ {
t_stacks *stacks; t_stacks *stacks;
@@ -32,18 +32,17 @@ int test1(int argc, char **argv)
stack_clear_all(stacks->a, stacks->a); stack_clear_all(stacks->a, stacks->a);
if (stacks->b) if (stacks->b)
stack_clear_all(stacks->b, stacks->b); stack_clear_all(stacks->b, stacks->b);
free(stacks);
return (0); return (0);
} }
int test2(char **argv){ int test2(char **argv)
{
int *tab; int *tab;
int len; int len;
t_tab *preset; t_tab *preset;
t_stacks *piles; t_stacks *piles;
int i;
i = 0;
len = ft_atoi(argv[2]); len = ft_atoi(argv[2]);
if (len < 1) if (len < 1)
{ {
@@ -51,16 +50,15 @@ int test2(char **argv){
return (0); return (0);
} }
tab = auto_shuffle(len); tab = auto_shuffle(len);
while (i < len)
{
printf("tab(%d) [%d]\n", i, tab[i]);
i++;
}
piles = init_big_stacks2(tab, len); piles = init_big_stacks2(tab, len);
printf("RANGE BUCKET %d\n", range_bucket(piles->a));
preset = get_tabs(piles->a, range_bucket(piles->a)); preset = get_tabs(piles->a, range_bucket(piles->a));
print_tabs(preset); print_tabs(preset);
free(tab); free(tab);
free_tab(preset); free_tab(&preset);
if (piles->a)
stack_clear_all(piles->a, piles->a);
if (piles->b)
stack_clear_all(piles->b, piles->b);
free(piles);
return (0); return (0);
} }