Suppose I have the following linked list:
struct ListNode {
int value;
struct ListNode *next;
}
And I want to free the memory of my linked list list using the function delete_list:
void delete_list(struct ListNode *list) {
while (list != NULL) {
struct ListNode *temp = list;
list = temp->next;
free(temp);
}
}
int main() {
struct ListNode *list = create_digit_list(56458);
list_print(list);
// while (list != NULL) {
// struct ListNode *temp = list;
// list = temp->next;
// free(temp);
// }
delete_list(list);
list_print(list);
return 0;
}
When I free my list in the same scope I get that list is NULL.
But for some reason when using the free my linked list using the my function delete_list and passing my list as a parameter, list isn't NULL and as a result the code prints an infinite loop of garbage.
What's the difference between the two ways? what's the correct way to extract the free operation to function?