How is this possible?

This is a discussion on How is this possible? within the C Programming forums, part of the General Programming Boards category; I'm trying to implement a b-tree and have hit a perplexing roadblock. Here's a section of code, it is pretty ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    How is this possible?

    I'm trying to implement a b-tree and have hit a perplexing roadblock. Here's a section of code, it is pretty simple and the whole purpose is debugging/to examine the nodes:
    Code:
    195 |___while (cur) {
    196 |___|___fprintf(stderr,"\t%s ->%p %p\n", cur->val,cur->ls,cur->rs);
    197 |___|___ray[i] = cur->ls;
    198 |___|___para[i++] = cur->val;
    199 |___|___if (!cur->next) ray[i] = cur->rs;
    200 |___|___cur = cur->next;
    201 |___}
    So I get a seg fault at line 200. The only possible reason AFAIK would be if cur is NULL. Which I do not see how that could happen based on the while condition, so I checked it out in GDB:

    Breakpoint 4, topdown (n=0x602250, from=0x0) at b-tree2.c:199
    199 if (!(cur->next)) ray[i] = cur->rs;
    (gdb) print cur
    $5 = (key *) 0x6022b0
    (gdb) print cur->next
    $6 = (struct _key *) 0x0
    (gdb) step
    200 cur = cur->next;
    (gdb) print cur
    $7 = (key *) 0x0
    (gdb) print cur->next
    Cannot access memory at address 0x10

    So of course the next thing is the seg fault. But how can the address in cur change from 0x6022b0 to NULL because of this instruction:

    if (!(cur->next)) ray[i] = cur->rs;

    ????????????????
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hey, I bet the size of one of those arrays is to blame...heh heh.

    ....yep
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21