pointer copy causes data corruption inside node

Hello.

This is a frequent problem I encounter and I want to be able to figure out what is actually going on.

I have a node class that has char and char* private members.

When I am trying to delete a node, the node actually goes away, but the nodes that follow the deleted node get corrupted. Both the char and char* data in the node is gibberish or NULL. They don't go away, as there are correct number of nodes after the delete, but the data inside nodes are acting up.

I know that the problem is something to do with the head = temp statement. I thought this was a safe copy method between two pointers.

Can you see what is going on ? How can I better deal with this.



TAKEN FROM A RECURSIVE FUNCTION
1
2
3
4
5
6
7
8
9
10
                node*temp = head->next_node(); // Assign temp to next pointer
                
                temp->node_display();// Temp Points to the correct node
                delete head;
                
                head = temp; // Problem starts after this line
             
                head->node_display(); // Node is now corrupted.
                return;


PROTOTYPE OF FUNCTION THAT RETURNS NEXT
1
2
                node*&  next_node();


THIS IS THE FULL FUNCTON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

 if(!head)
        {
                
                return;
        }

        if (head->compare_elements(u_node))
        {
                
                node*temp = head->next_node();
                
                temp->node_display();
                delete head;
    
                head = temp;
                head->node_display();
                return;
        }
        else
        {       
                return remove_LLL(head->next_node(), u_node);
        }

Last edited on
Topic archived. No new replies allowed.