Hi I’m trying to make a function that clears a linked list that *first will point to, then the node **first should be freed and the pointer *first set to NULL.
I’m having trouble grasping double pointers and can’t get this to work correctly.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have to move to the next list element before you delete the node. Otherwise you are accessing memory that has been freed.
Be aware that because you’re trashing the whole list,
*firstis going to eventually be NULL. So you can use a single-pointer (just liketemp) to traverse your list, and then set*first = NULLat the end. That saves an extra pointer indirection, which arguably is wasteful of CPU in this case.[edit] What I mean is:
Generally, I find that the less pointer dereferencing you have going on, the easier the code is to understand at a glance.