in Programming in C retagged by
602 views
0 votes
0 votes

DOUBT 1:

ImagePreview

 

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

ImagePreview

 

DOUBT 2:

what does head = (*P).link;  equals to?

in Programming in C retagged by
602 views

22 Comments

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
1
edited by

Aditya_

So ,  the resultant L.L is correct then,  following that operation?

0
0

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->s

Yes resultant L.L is correct.

1
1
edited by

Doubt 2: (*(*(*p).q).r).s is equivalent to a->b->c->d

you 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
0

@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
0

@Aditya_ then it is wrong?

0
0

@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
1
0
0

    

😅😅  small things create confusions.

0
0

@Abhrajyoti00

so if head pointer is removed and now no pointer is pointing to the nodes a and b

then now is it not the dangling pointer problem?

0
0

@Abhrajyoti00 Isn't it memory leak rather then dangling pointer?

 

0
0

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
0

@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
2

@Aditya_

suppose i have three nodes like  

1 <-> 3 <-> 4

now 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 3

so is it also memory leak?

as here also we are not freeing the node . just updating the links.

0
0

@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
0

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
0

@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
0

@Aditya_ what is the difference between deallocate & delete in memory?

0
0

@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
3

@Aditya_ Very nicely written :)

1
1

@Aditya_ well explained👍

1
1

@Aditya_

Amazing bro!!

Thanks! 

1
1

Please log in or register to answer this question.

Related questions