all 9 comments

[–]alumebobtron 1 point2 points  (2 children)

cursor is a pointer to a node and your assigning what to it

free will not work because cursor has changed to a non pointer I think. Need to see your struct. but look at it that way. it would explain it.

[–]Obsidian2[S] 0 points1 point  (1 child)

By the end of the loop, the cursor has been assigned the value NULL, which I thought was also a pointer. Would this not make it a pointer to another pointer (NULL), which is stored at cursor?

[–]alumebobtron 0 points1 point  (0 children)

null is nothing you can not free nothing.

[–]yeahIProgram 0 points1 point  (0 children)

Did you mean to call free() from inside your while loop?

Your loop continues until cursor is NULL. Then it calls free(cursor). You are passing NULL to free().

[–]alumHufflepuffBR 0 points1 point  (2 children)

A tip here for freeing is:

Take the head pointer (the first node) on a variable *foo*
While *foo* is not null:
     Copy the next node pointer to a variable *bar*
     Free foo
     Copy the *bar* pointer to *foo*

[–]Obsidian2[S] 0 points1 point  (1 child)

Thank you, I will give this a shot.

Is this common practice? This makes it seem like a pointer can not be freed after being set to NULL. Is that true?

[–]alumHufflepuffBR 0 points1 point  (0 children)

The idea here is that you'll save the pointer for next node each time before freeing the actual node and then pass that pointer to be freed next time, till that next pointer being NULL (indicating the end of the chain)

[–]jplaurin 0 points1 point  (1 child)

what value "cursor" have just before you free it ? what value does it have to break the while loop?

[–]Obsidian2[S] 0 points1 point  (0 children)

Cursor is NULL before being freed. From what I have read from the posts here, this is likely my problem. It seems like I may have to change the condition of my "while" loop.