Segmentation Fault Issues

While debugging I keep getting a segmentation fault when I come to this portion of my .cpp file. Is there anyway someone can help me fix this error because it's frustrating and I don't know where it's coming from.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string List::Longest()
{
    int size = 0;
    string sizestring = Head->Data;
    Node* Current = Head;
    if (Current == NULL)
        return "Null";
    while(Current->Next != NULL)
    {
        
        if ((Current->Data).length() > size)
        {
            size = (Current->Data).length();
            sizestring = Current->Data;
        }
        Current = Current -> Next;
    }

    return sizestring;
}
Which debugger are you using that doesn't tell you which line causes the segFault? Are you building with debugging information switched on?

This problem is exactly what a decent debugger will identify for you in seconds.
It's a NetBeans debugger and it doesn't say which line the seg Fault is caused by. It just says segfault and aborts. I know it's this function though.
Almost certainly, you're trying to read or write memory that doesn't belong to you, because you've got a bad pointer. Without a working debugger to tell you exactly where, you'll just have to output the value of each pointer you're using and see which of them is bad.
The segmentation fault came as a result of having

string sizestring = Head->Data;

before the if statement which was a segmentation fault if the head was Null.
Topic archived. No new replies allowed.