0

I've got a problem, because I don't know how to delete all nodes in that kind of list. What do you think about my solution? Debugger shows me a problem "(...) x was 0xDDDDDDDD"

void del(List *&head) {
    List* ptr = head;
    List* help;
    do {
        List *x = ptr;
        List *y = ptr;
        x = y;
        help = x;
        x = x->next;
        delete y;
        head = NULL;
        ptr = ptr->next;

    } while (help!=NULL);
user76234
  • 43
  • 1
  • 5
  • 1
    Only `delete` what you `new`. How is your `List` allocated? Please provide [mcve]. – Algirdas Preidžius Nov 12 '18 at 13:16
  • 1
    Please give your variables names that make sense. See: https://stackoverflow.com/a/853187/3212865 – spectras Nov 12 '18 at 13:20
  • 1
    You have a node pointer called "help"? What does that signify? – Lightness Races in Orbit Nov 12 '18 at 13:24
  • There does not seem to be a need for so many variables. Could you please clarify what is your intended purpose for each? – GoodDeeds Nov 12 '18 at 13:29
  • Also, I suspect the idea of using `while(help != NULL);` is to check if an element has already been deleted. However, just because an element has been deleted does not make it `NULL`, and accessing that memory after deletion is undefined behaviour. – GoodDeeds Nov 12 '18 at 13:30
  • 2
    Removing the unnecessary variable juggling, your loop is equivalent to `do { help = ptr; delete ptr; ptr = ptr->next; } while (help != NULL);`. Can you spot the bad dereference? – molbdnilo Nov 12 '18 at 13:34
  • *because I don't know how to delete all nodes in that kind of list.* -- If you have already written a function to delete the first node, then you already know how to delete all nodes. Just write a loop deleting the head node until there are no more nodes to delete. `while (head_node) { remove(head_node); }`, or something similar to that. In other words, reuse what you've already written (*if* you have written such a function to remove a single node). – PaulMcKenzie Nov 12 '18 at 13:50

2 Answers2

0
List *y = ptr;
delete y;
ptr = ptr->next;

The memory allocated at ptr is gone after delete y. You can not call ptr->next as ptr is dangling at this point and de-referencing it is undefined behavior. Try reordering the last two lines i.e.

ptr = ptr->next;
delete y;
Manoj R
  • 3,197
  • 1
  • 21
  • 36
0

Here is the complete example with the test

struct Node
{
    Node(Node* prev, int val)
        : prev(prev), next(NULL), value(val)
    { }
    Node *prev, *next;
    int value;
};

void clear_list(Node*& head)
{
    if (head != NULL)
    {
        Node* curr = head->next;
        while (curr != NULL && curr != head)
        {
            std::cout << "Deleting " << curr->value << std::endl;
            Node* temp = curr;
            curr = curr->next;
            delete temp;
        };
        delete head;
        head = NULL;
    }
}

void print_list(Node*& head)
{
    if (head != NULL)
    {
        Node* curr = head;
        do
        {
            std::cout << (curr == head ? "Head: " : "") << curr->value << std::endl;
            curr = curr->next;
        } while (curr != NULL && curr != head);
    }
}

int main()
{
    Node* head = new Node(NULL, 0);
    Node* curr = head;
    for (int i = 1; i <= 10; i++)
    {
        Node* prev = curr;
        curr = new Node(prev, i);
        prev->next = curr;
    }
    // Link end to head
    curr->next = head;
    //
    print_list(head);
    clear_list(head);    
    print_list(head);
}
serge
  • 992
  • 5
  • 8