Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

what the flying fuck

Name: Anonymous 2008-12-02 20:12

This C++ code is causing a segfault, and I can't understand why. Yes, it's homework, but I don't want my homework done. I just want the one bug that's pissing me off and that I can't fix fixed. You'd think it's the pointer fucking up, but apparently not. Here's the code causing the issue, with printf's to figure out where the hell it's fucking up:

printf("attempting get next\n");
list = list->getNext();
list->print();
printf("attempting remove previous\n");
list->removePrevious();
printf("success!\n");

The list->print(); is to ensure that the pointer's pointing at something of value. This is never what actually fucks it up, so apparently the pointer's not pointing off into nowhere.

Here's the code for removePrevious(), again with debugging printf's:

printf("deleting previous");
previous->setPrevious(NULL);
previous->setNext(NULL);
delete previous;
printf("...done\n");
previous == NULL;

It never actually prints out "deleting previous". It evidently segfaults right when it tries to call removePrevious().

Name: Anonymous 2008-12-02 20:54

>>4
Okay, I fixed that. Here's the new removePrevious():

void LinkedList::removePrevious()
{
    printf("starting removePrevious()");
    if(next != NULL)
    {
        LinkedList *tempNode = previous->getPrevious();
        next->setPrevious(NULL);
        next->setNext(NULL);
        delete next;
        next = tempNode;
        previous->setNext(this);
    }
    printf("...done\n");
}
Here's the output:

Please input a choice: d 1
attempting get next
Value: 62
attempting remove previous
Segmentation fault

"d 1" is my input, telling it to delete the first node.
"attempting get next" is right before it tries to use getNext().
"Value: 62" is from print().
"attempting remove previous" is from the step immediately before calling removePrevious().
...and then segfault.

Somehow, it never gets to printing "starting removePrevious()". It's the call to removePrevious() itself that seems to be causing the segfault somehow.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List