Program compiles but crashes

solved
Last edited on
It will crash on line 29. Change it to: while(last != NULL) { // Note: remove ->link
Actually, line 29 needs to be as it is -- while(last->link != NULL) -- as last must be non-null when the while loop exits so that last->link can be set to point at the new link on line 34 (rather than crashing as last is null!)

It compiles, but crashes when I run it

But what's it doing when it crashes? Have you a minimal test case for us?

And the definition of IntNode while you're at it (IntNode.h)

Actually, it doesn't look like what you've posted will compile even if IntNode.h is provided as neither void IntList::dublicate() nor void dublicateHelper(head) are declared in the class definition?

Andy

PS I would move the declaration of last inside the else block as it's not needed earlier.

24
25
26
27
28
29
30
31
32
33
34
35
36
void IntList::append(int n) {
    if(head == NULL) {
        head = new IntNode(n);
    } else {
        NodePtr last = head;
        while(last->link != NULL) {
            last = last->link;
        }
        NodePtr temp = new IntNode();
        temp->data = n;
        last->link = temp;
    }
}
Last edited on
Topic archived. No new replies allowed.