head and head points to the linked list 7->0->8->0. I traverse the list using head and it reaches the last node with value 0. When I do head = NULL (that means make the last node disappear) and traverse the list now with help of ptr then ptr is still able to access the nulled last node. Why?
That doesn't mean you make the last node disappear, you just manipulate the location that your pointer pointed to, not the actual memory of that location.
ohh! So shud I do "delete head" to not allow ptr access the last node but the rest of the list?
But it's throwing error saying that "heap-use-after-free on address blah". Look ptr = head and ptr stands at the first node while the head has reached the last one and been deleted.
Honestly, I have no way of knowing what you have done. Here is an example simplified without taking into account any best practices: ix.io/3WEb
Обсуждают сегодня