简体   繁体   中英

Segmentation fault in linked list

Edit: Dijkstra's answer is the way to go about fixing this problem. My list wasn't initialised to NULL

I am working on a linked list to store a unique list of words, which segfaults when I attempt to traverse the list. Gdb gives me:

    Program received signal SIGSEGV, 
    Segmentation fault. 0x0000003a07e47ff7 in vfprintf () from /lib64/libc.so.6 
    Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6_0.5.x86_64

The insert code for the list is:

typedef struct L { char x[40] ; int occ; struct L *next ;} List;
List *insertList( char *in, List *l )
{
    List *t = calloc( 1, sizeof( List ) ) ;
    strcpy(t->x, in);
    t->occ = 1;
    t->next = l ;
    return t ;
}

void printList(List *l)
{
    List *l2 = l;
    while(l2)
    {
        printf("%s ", l2->x);
        l2 = l2->next;
    }
    return;
}

Its looping through the words, inserting them into the linked list, seemingly fine. When I loop through the list to display the words (of about 4200 words), about 98%ish will display fine, then it will segfault without warning.

With some more inspection, its reading back the words in the opposite order to which they were added (which makes sense) and will reach about the 5th word from the end of the list (the 5th word added) before segfaulting. I have tried adjusting the insert function to allow for strings more than 40 chars long, but the words being inserted at the beginning (and being segfaulted) are all under 20 chars.

With even more digging, if I printf l2->next->next->next->next in the printList function, the first words inserted are there.

Could anyone possibly point me in the right direction with this?

Thanks

gdb (or another debugger if you aren't using linux) is my tool of choice for tracking down segfaults. Compile the code with debug symbols and run it in the debugger. When you crash, inspect the line that is causing the crash. Use the backtrace command as needed. Following these steps pretty much always shows me how to fix a segfault.

My best guess would be that strncpy instead of strcpy would fix your problem, it sounds like something has overwritten your 'next' pointer somewhere late in the list, and too long a string in 'in' would do that for sure.

Don't forget that strncpy won't terminate strings that are too long, so to make sure to put

x[39]=0;

to make sure the string will be properly terminated.

How are you initialising your first node?

You say "the first few words are causing the seg fault", but the interruption may stop them printing and the problem is actually coming right at the very end.

My hypothesis (and it's really just a guess :P) is that your first node doesn't have next = NULL; , it's just uninitialised memory. Therefore the while loop doesn't detect that it's reached the end of the list and tries to print something bizarre, resulting in a segfault.

This is a stylistic remark (which won't be appreciated by the SO folks ;-[ ) What is wrong with a plain for loop? Why do you iterate inside the loop, when there is a valid idiomatic construct for this kind of thing?

void printList(List *lp)
{
    List *l2;

    for(l2=lp; l2; l2 = l2->next)
    {
        printf("%s ", l2->x);
    }
    return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM