-1

I am working with algorithms and got a doubt about deallocating pointers, specifically with removing the first element of a linkedlist, here is the method:

template<typename T>
class LinkedList
{
    {
        T value;
        Node *next;
        Node(T value)
        {
            this->value = value;
            this->next = nullptr;
        }
        Node(T value, Node *next)
        {
            this->value = value;
            this->next = next;
        }
        ~Node()
        {
          
        };
    };

public:
    Node *head;
    Node *tail;
  
    LinkedList()
    {
        this->head = this->tail = nullptr;
    }

    ~LinkedList()
    {
        Node *curr = this->head;
        while (curr != nullptr)
        {
            Node* next = curr->next;
            delete curr;
            curr = next;
        }
    }

    void addFirst(T value)
    {
        Node *newNode = new Node(value);
        if (!this->head)
        {
            this->head = this->tail = newNode;
            return;
        }
        newNode->next = this->head;
        this->head = newNode;
    }

  

    void removeFirst(){

        if(!this->head) return;

        Node* tmp = this->head->next;
        delete this->head;
        this->head = tmp;
    }
};

My question is, since tmp points to the next node of head, when deallocating the head, it frees the memory (the value of the head and the reference to the next node). Would that next node become invalid? Could someone explain me further what happens exactly when doing delete this->head?

Andfernan
  • 57
  • 4
  • 1
    You'll probably be left with a dangling pointer and a memory leak. – πάντα ῥεῖ May 26 '23 at 16:39
  • Draw it out on paper and it will all make sense. Your code is not complete since removeFirst is not a member of a list class. Also note all this `this->` is not needed. Also do not forget to initialize your pointers to nullptr at start. – Pepijn Kramer May 26 '23 at 16:40
  • 1
    Since the node doesn't delete its next, it's perfectly safe and fine. – ChrisMM May 26 '23 at 16:47
  • A note to think about later: the only difference between `head` and `next` is the name. If you abstract away that one difference you can eliminate a lot of `head`-specific code. The [Alternative Using Pointer To Pointer](https://stackoverflow.com/a/22122095/4581301) in this answer is a great example of that abstraction in action. – user4581301 May 26 '23 at 16:57
  • 2
    Leaving aside the problems with the code, what happens when you `delete head;` is that head's destructor (i.e. `Node::~Node()`) is called and then the memory occupied by `head` is released. So whether `head->next` is affected or not, depends entirely on what is in that destructor, which unfortunately you don't seem to have shown us. If `Node` does not have a destructor then the answer is that nothing happens to `head->next`. – john May 26 '23 at 17:48
  • 1
    Remember that two different pointers can point to the same location in memory. Blowing away one of those pointers doesn’t do anything to what’s at that memory location, nor does it alter the other pointer. – pjs May 26 '23 at 17:50

1 Answers1

1

AFAIK, deleting this->head deletes the pointer to the next node, but not the actual memory of the next node. Deleting this->head the way you've done is fine.

azhen7
  • 371
  • 1
  • 5
  • 2
    And this could be confirmed if we could see the definition of `Node`. For all we know it contains `~Node () { delete next; }`, and if it does, `delete head;` will most definitely have wider-reaching effects. All we can say for certain at this point is... "I dunno." – user4581301 May 26 '23 at 19:55
  • 1
    A few hours before this answer was posted, the asker had already added the definition of `~Node`, so the answer is fine (+1). – trincot May 27 '23 at 10:39