My clean_stash(list **stash) function takes a pointer to a linked list
the linked list contains strings of buff size which are extracted from a bigger string str.
for example:
typedef struct node
{
char *content;
struct node *next;
} list;
char *str = "atomic\nhabits";
int buff = 5;
by that the linked list stash will contain:
------- --- -------- --- ----- ------
stash | atomi | @ |---->| c\nhab | @ |---->| its | NULL |
------- --- -------- --- ----- ------
// let's don't care about the code behind filling the linked list, I tested it and it works fine
the clean_stash function will delete the all the text but the last line
so the result will be:
---------- ------
stash | habits\0 | NULL |
---------- ------
this is my clean_stash function:
void clean_stash(list **stash)
{
list *last;
list *clean_node; // from ato\nmic to mic
int i;
int j;
clean_node = malloc(sizeof(list));
if (!stash || !clean_node)
return ;
clean_node->next = NULL;
last = ft_lst_get_last(*stash);
i=0;
while (last->content[i] && last->content[i] != '\n')
i ;
if (last->content && last->content[i] == '\n')
i ;
clean_node->content = malloc(sizeof(char) * ((strlen(last->content) - i) 1));
if (clean_node->content == NULL)
return ;
j = 0;
while (last->content[i])
clean_node->content[j ] = last->content[i ];
clean_node->content[j] = '\0';
lst_display(clean_node); //this works as expected
free_stash(*stash);
*stash = clean_node;
lst_display(clean_node); //this doesn't work at all
}
I created a clean_node of type list and coppied in it the last line in stash and all of that worked fine. I used lst_display function to display the content of the clean_node and I got my expected output(a node containing the last line)
the problem started when I freed the stash pointer and tried to assigne the clean_node to it. I used a free_stash(*stash) function.
void free_stash(list *stash)
{
list *current;
list *next;
current = stash;
while (current)
{
free(current->content);
next = current->next;
free(current);
current = next;
}
}
and that deleted both my stash and my clean_node. I don't know why it deleted my clean_node.
and that's what confused me for 3 days :)
I thought the problem is in this line clean_node->content[j ] = last->content[i ];
so I changed it with strcpy(clean_node->content, (last->content i)); but that didn't work too
I need help please can anyone explain this to me?
Thanks in advance.
Edit:
ft_lst_get_last return a pointer to the last element in the list:
list *ft_lst_get_last(list *stash)
{
list *current;
current = stash;
while (current && current->next)
current = current->next;
return (current);
}
lst_display just displays a linked list:
int lst_display(list *lst)
{
int i;
i = 0;
printf("/////////// list /////////////\n");
while (lst)
{
printf("%d: %s\n", i, lst->content);
lst = lst->next;
i ;
}
printf("///////// end of List //////////\n");
return (i);
}
CodePudding user response:
for some reason my free_stash function was deleting my clean_node list while I was trying to free the stash list, clean_node and stash are two separate lists they're not related to each other at all. despite of that it get's deleted when I pass stash to the free_stash()
I still need an explanation why that happened
but I solved the problem with changing my free_stash function to
void free_stash(list **stash)
{
list *current = *stash;
list *next;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
*stash = NULL;
}
