I am trying to print my linked list until the next node is NULL but I am seg faulting after printing two nodes. mov is my linked list nodes.
while(mov->next != NULL)
{
printf("%s\n", mov->movie_title);
printf("%s\n", mov->...etc...)
free(mov);
mov = mov->next;
}
This is my push function that I use to assign the data to the nodes.
void push(movie** mov)
{
movie* new = malloc(sizeof(movie));
new->next = (*mov);
(*mov) = new;
}
After assigning the data to the node, I call push(&mov);
I was using an array of structs before but since the amount of memory needed to store became very large, I thought it wouldn't work very well since malloc allocates a contiguous block of memory where as with nodes, I can allocate small chunks of memory for each node.
mov->next
is NULL for the last node?free(mov);
and afterwardsmov = mov->next;
. You may not access data which has beenfree
-d.