0 votes 0 votes DOUBT 1: if head = P → link. is performed then what will happen to the nodes containing values a and b? will they get removed as no link is pointing them? and we will left with only three nodes like DOUBT 2: what does head = (*P).link; equals to? Programming in C data-structures linked-list doubt + – Pranavpurkar asked Sep 27, 2022 • retagged Sep 27, 2022 by Shubham Sharma 2 Pranavpurkar 594 views answer comment Share Follow See all 22 Comments See all 22 22 Comments reply Shoto commented Sep 27, 2022 reply Follow Share Doubt 1: Node a and node b will be in the memory but we will not be able to access them. Doubt 2: It is equivalent to P → link 1 votes 1 votes Pranavpurkar commented Sep 27, 2022 i edited by Pranavpurkar Sep 27, 2022 reply Follow Share Aditya_So , the resultant L.L is correct then, following that operation? 0 votes 0 votes Abhrajyoti00 commented Sep 27, 2022 reply Follow Share Extra points:-Doubt 1: This problem where nodes remain in memory but we can’t access gives rise to Dangling Pointer problem. Doubt 2: (*(*(*p).q).r).s is equivalent to p->q->r->sYes resultant L.L is correct. 1 votes 1 votes Pranavpurkar commented Sep 27, 2022 i edited by Pranavpurkar Sep 27, 2022 reply Follow Share Doubt 2: (*(*(*p).q).r).s is equivalent to a->b->c->dyou mean p→ q→ r→ s ? or if a→ b→ c→ d is correct then is p pointing to the node containing value a and similarly others pointing to b,c,d? 0 votes 0 votes Pranavpurkar commented Sep 27, 2022 reply Follow Share @Abhrajyoti00 Doubt 1: This problem where nodes remain in memory but we can’t access gives rise to Dangling Pointer problem. is this because we are indirectly freeing up them(nodes)? 0 votes 0 votes Nisha Bharti commented Sep 27, 2022 reply Follow Share @Aditya_ then it is wrong? 0 votes 0 votes Abhrajyoti00 commented Sep 27, 2022 reply Follow Share @Pranavpurkar Refresh the page. I removed that typo within 1 min 😂 Yes. $free(pointer)$ is a better practice.@Nisha Bharti No no. That is absolutely correct. head = head->next is the most famous opeartion in every L.L q. Just that if some memory is dedicated to pointer but we cant access it, it’s called dangling pointer.See this : C: How to free nodes in the linked list? - Stack Overflow 1 votes 1 votes Nisha Bharti commented Sep 27, 2022 reply Follow Share @Abhrajyoti00 ok :( 0 votes 0 votes Pranavpurkar commented Sep 27, 2022 reply Follow Share Abhrajyoti00 😅😅 small things create confusions. 0 votes 0 votes Pranavpurkar commented Sep 27, 2022 reply Follow Share @Abhrajyoti00so if head pointer is removed and now no pointer is pointing to the nodes a and bthen now is it not the dangling pointer problem? 0 votes 0 votes Shoto commented Sep 28, 2022 reply Follow Share @Abhrajyoti00 Isn't it memory leak rather then dangling pointer? 0 votes 0 votes Pranavpurkar commented Sep 28, 2022 reply Follow Share Aditya_ when head is still pointing to the node containing value a so i think saying it as a dangling pointer problem is correct! but when head is changed to d at that time no pointer is pointing to the nodes having values a and b at that time i think it may lead to memory leak but can only two extra nodes actually lead to memory leak? 0 votes 0 votes Shoto commented Sep 28, 2022 reply Follow Share @Pranavpurkar Before destroying node ‘a’ (freeing up that space) if head is pointing to node ‘a’ is it a dangling pointer? Memory leak is when we allocate some space and forgets to de-allocate it, it above case we created node ‘a’ and node ‘b’ but before changing the pointer we forgot to de-allocate it so now there is no way to free that space hence leads to memory leak. 2 votes 2 votes Pranavpurkar commented Sep 28, 2022 reply Follow Share @Aditya_suppose i have three nodes like 1 <-> 3 <-> 4now to delete 3 (a pointer p is pointing to this node) we just change the next of prev node(to be deleted) and change the prev of the next node. so now no link is pointing to the node having value 3so is it also memory leak?as here also we are not freeing the node . just updating the links. 0 votes 0 votes Shoto commented Sep 28, 2022 reply Follow Share @Pranavpurkar p is still pointing to node 3 right, if you change pointer p without deleting node 3 then yes there will be a memory leak 0 votes 0 votes Pranavpurkar commented Sep 28, 2022 reply Follow Share Aditya_ no p is not changed just the node is excluded from the present DLL in that case is it still memory leak or dangling pointer(but we are not freeing up any node)? if you change pointer p without deleting node 3 do you mean change pointer p after deleting node 3 right? because without deleting node 3 it is still present in the DLL so even after changing p it will not affect it . 0 votes 0 votes Abhrajyoti00 commented Sep 28, 2022 reply Follow Share @Aditya_ Yes you are correct. It will be memory leak not dangling pointer. Actually dangling pointer is said to a pointer. Here there is no pointer. Rather its a phenomenon where a space is allocated (like in a heap) and we didn’t de-allocate the space. Thanks for pointing out the mistake bro. 0 votes 0 votes Nisha Bharti commented Sep 28, 2022 reply Follow Share @Aditya_ what is the difference between deallocate & delete in memory? 0 votes 0 votes Shoto commented Sep 28, 2022 reply Follow Share @Pranavpurkar I am writing in the more simple way Here let’s say if change $head$ pointer to point to $p$ without deleting the node $a$ and node $b$, so we won’t have any means to access node $a$ and node $b$ so there will be a memory leak but there is no dangling pointer. If I first delete the node $a$ and $b$ (i.e. I free up the memory) then change the $head$ pointer to $p$ there will be no memory leak neither there will be any dangling pointer. If I delete the node $a$ and $b$ (i.e. I free up the memory) and do not change $head$ pointer, now I do not have access to the memory location where node $a$ and $b$ was present because I freed the memory then $head$ pointer will be the dangling pointer because it still points to the same memory location which I just freed. So if I try to de-reference the $head$ pointer I will get segmentation fault. So in this case memory leak is not there but dangling pointer is there. @Nisha Bharti I am using them interchangeably, both means the same that I free that memory location so that it can be used later. 3 votes 3 votes Abhrajyoti00 commented Sep 28, 2022 reply Follow Share @Aditya_ Very nicely written :) 1 votes 1 votes Nisha Bharti commented Sep 28, 2022 reply Follow Share @Aditya_ well explained👍 1 votes 1 votes Pranavpurkar commented Sep 28, 2022 reply Follow Share @Aditya_ Amazing bro!! Thanks! 1 votes 1 votes Please log in or register to add a comment.