0

Why we initialize the next pointer of Linked List as NULL before deletion we move our head to the next node during deletion and we free the memory of the first node, so why we need to initialize the next pointer of deleted node as NULL before deletion. without it, the code runs without any issues. Is this dangling pointer will create a issue? please throw some light in it

class Node
{
    public:
    int data;
    Node* next;
    
    Node(int d)     //construtor for storing the value in nodes
    {
        this->data=d;
        this->next=NULL;
    }
};
void DeleteAt(int position,Node* &head,Node *&tail)
{
    if(position ==1)
    {   
        Node* temp=head;
        head=temp->next;
        temp->next=NULL;
        delete temp;
    }
    else
    {
        Node *curr=head;
        Node *prev=NULL;
        int cnt=1;
        while(cnt<position)
        {
            prev=curr;
            curr=curr->next;
            cnt++;
        }
        // if delete at tail is called ,for updation of tail,
        //if required
        if(curr->next==NULL)
        {
            tail=prev;
        }
        prev->next=curr->next;
        curr->next=NULL;
        delete curr;
    }
}
  • 2
    You don't need to unless some maniac took it upon themselves to be "smart" and code the destructor of a `Node` to auto-magically loop or recurse to destroy the rest of the chain starting at `next` . self-preservation step. your code doesn't do that (which is good, btw), so it is pointless here. – WhozCraig Oct 14 '22 at 17:54

1 Answers1

0

You don't need to do this, it's just to help you while you're learning. It's common for teachers to do this to help avoid use-after-frees. This way, it's much more obvious you have a problem, because if you accidentally try to use the node you just deleted, you'll probably get a segfault.

CoffeeTableEspresso
  • 2,614
  • 1
  • 12
  • 30
  • we are freeing the memory which is pointed by temp i.e we are deleting the data and stuffs inside it too, right? and next pointer is pointing to the next node, So when we delete the temp , the pointer next will also get deleted so the memory which is pointed by next (i.e. Next Node) should be deleted too, I know I sound not good but can u clarify it more if you got my point –  Oct 14 '22 at 17:47
  • 1
    Dereferencing a null pointer is not guaranteed to give you a segfault. [Is segfault guaranteed when dereferencing null pointer](https://stackoverflow.com/questions/46104370/is-segfault-guaranteed-when-dereferencing-null-pointer-c-c). – Jason Oct 14 '22 at 17:51
  • @VishalMourya The next pointer is only going to get deleted if that's done in the destructor. Since you didn't declare a destructor that does this, it's only going to delete the one `Node`. Nothing happens to the object at the `next` pointer. – Spencer Oct 14 '22 at 17:54
  • @JasonLiam, based on the question, I would say this is probably OP's first C++ course, so I don't think it's appropriate to get into details about UB at this point. – CoffeeTableEspresso Oct 15 '22 at 00:13
  • @CoffeeTableEspresso It is irrelevant whether or not OP is a beginner. You cannot still claim that dereferencing a null pointer will give you a segfault. – Jason Oct 15 '22 at 04:51