LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   segmentation fault w/ c++ function call (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-w-c-function-call-6933/)

wsimmons 09-26-2001 11:23 PM

segmentation fault w/ c++ function call
 
I am getting a segmentation fault (core dumped) when a program that I am writing calls the following function:

void test_fx()
{
MainDataSetPtr here_ptr;//local scope
here_ptr = head;
do
{
cout<<head->d_structCloneID<<"\n";
cout<<head->d_structSubclone<<"\n";
//a few more struct variables that I don't want to
//take the time to write at the moment
here_ptr = here_ptr-> link_down;
}while (here_ptr-> link_down != NULL);
return;
}

The struct worked fine in other functions (I can load it with data)that I have used in the porgram. but now that I am trying to extact the data from it for processing (including file output streams) there seems to be difficulty in the programming. Any help with this? Thank you.

isajera 09-27-2001 01:18 AM

where exactly is the program seg faulting? is it printing anything out at all, or does it just dump immediately?

pinoy 09-28-2001 05:59 PM

This is the same post I replied to at Linuxnewbie.org. I'll give you the answer this time though.

Code:

    here_ptr = here_ptr-> link_down;
}while (here_ptr-> link_down != NULL);

Never dereference a NULL pointer.

Code:

    here_ptr = here_ptr-> link_down;
    if (here_ptr == NULL)
        break;
}while (here_ptr-> link_down != NULL);


karakonchar 10-16-2001 02:06 AM

Lots of problems in the code. I assume that you intend to traverse and print a linked list. But the code always prints head node.

Seg. fault will occure if head is NULL. Or it might be occuring in the while condition. Change the while condition to following.

while (here_ptr) ;

With the condition you are using you will not be able to print the last node.


All times are GMT -5. The time now is 04:23 PM.