Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Why set object to null after deleting? [duplicate]

Tags:

c++

I am looking at the following piece of linked list code I found online:

void DeleteAfter(Node **head){
      if(*head==NULL){
            return;
      }else{
            Node *temp = NULL;
            temp = (*head)->next;
            (*head)->next = (*head)->next->next;
            delete temp;
            temp=NULL;
      }
}

I am not that skilled with C++, so this could be a bad question, but why is temp being set to NULL after being deleted? Is this a necessary step?

like image 583
John Roberts Avatar asked Jan 19 '13 17:01

John Roberts


People also ask

Do you have to set pointer to NULL after delete?

Setting pointers to NULL following delete is not universal good practice in C++. There are times when it is a good thing to do, and times when it is pointless and can hide errors. There are plenty of circumstances where it wouldn't help. But in my experience, it can't hurt.

Does deleting a pointer make it NULL C++?

In this case delete operation on one pointer would make only that pointer NULL (if delete was making pointer NULL) and the other pointer would be non-NULL and pointing to memory location which is free. The solution for this should have been that user should delete all pointers pointing to same location.

What is null object in C?

In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (null) behavior.

IS null pointer the same as uninstalled pointer?

Actually, Both acts same until and unless you assign some value to uninitialized pointer. For Ex: int *p; int *q=NULL; => both same. int *p; *p=3; int *q=NULL, *q=3; => In case of uninitialized pointer, 3 will be stored to some random/garbage location(uninitialized pointer), Hence you ll get value=3.


1 Answers

It's unnecessary. Some people make a habit of doing this even when it has no result. An aggressive compiler optimizer will eliminate this code, so it doesn't actually do any harm. But I would have written:

void DeleteAfter(Node *head) {
  if (head) {
    Node *next = head->next;
    if (next) {
      head->next = next->next;
      delete next;
    }
  }
}

Note I eliminated a useless level of indirection and added a check to make sure there's a "node after" to delete.

The rationale for the habit is that if a pointer always refers to either a valid object or is null, you can rely on null checks as equivalent to validity checks.

For this reason, Ada, a language often used in safety critical systems, initializes pointers to null and defines its delete equivalent operator to set its argument null automatically. Your C++ is simulating this behavior.

In practice, the value of this discipline is not what you'd hope. Once in a long while it prevents a silly error. One nice thing, however, is that debugger displays of pointer content make sense.

like image 153
Gene Avatar answered Oct 12 '22 23:10

Gene