-1

Following is a function for deleting the entire list by accessing the head pointer to the 1st node:

void deleteList(Node *head)
{
  while (head != nullptr)
  {
    Node *temp = head;
    head = head->next;
    delete temp;
  }
}

Detailed program:

struct Node {
  int data;
  Node* next;
};

void traverseList(Node* head) {
  while (head != nullptr) {
    cout << head->data << endl;
    head = head->next;
  }
}

void deleteList(Node *head)
{
  while (head != nullptr)
  {
    Node *temp = head;
    head = head->next;
    delete temp;
  }
}

int main() {
  Node* head = new Node();
  head->data = 10;
  head->next = new Node();
  head->next->data = 20;
  head->next->next = new Node();
  head->next->next->data = 30;

  traverseList(head);// traversing list before deletion
  deleteList(head);
  traverseList(head);// traversing list after deletion
}

Even after deleting the list, the program can access the list again, how is that possible?

Is there any other way to delete the list?

Is the delete keyword working properly? Or, do we not know how to delete the node and how the delete keyword works?

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • 5
    *"How it's possible?"* - undefined behavior. You wrote code that accesses invalid memory, so anything goes – UnholySheep Jul 11 '23 at 19:27
  • Your deleteList() is working correctly. There is a question if you want to pass the parameter by reference so that caller sees it set to `nullptr` after executing it. – drescherjm Jul 11 '23 at 19:31
  • Related: [https://stackoverflow.com/questions/44182049/pointers-in-c-after-delete](https://stackoverflow.com/questions/44182049/pointers-in-c-after-delete) – drescherjm Jul 11 '23 at 19:35
  • Also: [https://stackoverflow.com/questions/2896238/dereferencing-deleted-pointers-always-result-in-an-access-violation](https://stackoverflow.com/questions/2896238/dereferencing-deleted-pointers-always-result-in-an-access-violation) – drescherjm Jul 11 '23 at 19:36
  • 4
    Remember, after you `delete` an object its data isn't immediately removed from memory. The memory allocator just marks the memory that object was using as being free, and may use it to fulfill future allocation requests (at which point the new owner will presumably write over the old contents of that memory). – Miles Budnek Jul 11 '23 at 19:43
  • 3
    Similar issue: [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/q/6441218/4581301) – user4581301 Jul 11 '23 at 19:49
  • Do not tag `dsa`. Did you read what the `dsa` tag is for? – PaulMcKenzie Jul 12 '23 at 01:56

1 Answers1

2

Even after deleting the list, the program can access the list again, how is that possible?

Deleting an object invalidates any pointer that is pointing at it. Accessing an object through an invalid pointer results in undefined behaviour. When the behaviour of the program is undefined, "anything" is possible. You should avoid having undefined behaviour in your program. Refrain from attempting to access memory through invalid pointers.

Is the delete keyword working properly?

Yes.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
eerorika
  • 232,697
  • 12
  • 197
  • 326