Getting a double free error, I do not see it, what am I missing? (Solved) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Getting a double free error, I do not see it, what am I missing? (Solved)

Double free error when clearing the nodes of the linked list. https://code.sololearn.com/caR1K53CBvZm/?ref=app

22nd Sep 2023, 9:18 PM
William Owens
William Owens - avatar
2 Answers
+ 1
when freeing memory it is a good practice to set NULL to pointers that were freed to avoid this kind of problems ( it's a defensive style, protecting against dangling pointer bugs ). so you should do: ( line 90 ) .... free(current); current = NULL; .....
22nd Sep 2023, 11:11 PM
MO ELomari
0
In the function freenodes() you have an infinite while loop. The variable current has no way to get to a null value because it only changes when you assign to it the value of next, but you do it inside the if(next) block, so you never do it when next is null. Also you need to store the value of the pointer next before freeing current. Here's the code for the while loop to work: while(current) { next = current->next; free(current); current = next; list->cnt -= 1; printNodeCount(list); }
22nd Sep 2023, 11:10 PM
Giacomo Salvatore