What am I missing when freeing nodes in a C linked list?
#1
I’ve been trying to implement a simple linked list in C, but my program keeps crashing when I try to free the nodes. I think my logic for traversing and deleting is correct, but I must be missing something about how the pointers work during the deallocation process.
Reply
#2
I’ve had this exact issue. freed a node and then touched its next pointer because I hadn’t saved it first. It’s easy to crash when you’re looping with a raw pointer and don’t move to the next before free.
Reply
#3
I once forgot to set the head to NULL after the last free. A stray pointer kept being used somewhere and the program died when a free ran twice.
Reply
#4
I’ve also worried that the problem isn’t in the free loop at all but in how the nodes were allocated or linked. If you’re using a shared allocator or placing nodes in a region, you can end up freeing something twice or after already freed memory.
Reply
#5
Do you know if the crash happens right away on the first free or only after several frees?
Reply


[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Forum Jump: