segFault in linked-list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • philuu12
    New Member
    • Oct 2011
    • 14

    segFault in linked-list

    Hi,

    Could anyone please show me why the following segfault occurs
    at line 54 in the following code?

    Code:
         1  #include <stdio.h>
         2  #include <stdlib.h>
         3
         4  void printHello()
         5  {   printf("Hello ");  }
         6
         7  void printThere()
         8  {   printf("There\n");  }
         9
        10  void (*FuncPtrArry[2])() = {&printThere,  &printHello};  // Init function pointer array
        11
        12  typedef struct mystruct
        13  {
        14     struct mystruct *next;
        15     void (* funcPtr)();
        16  } NodeStruct;
        17
        18  NodeStruct *topNode = NULL;
        19  NodeStruct *lastNode = NULL;
        20  int count = 2;
        21
        22  void buildlist(void)
        23  {
        24     for (/* count already init to 2 */ ;count > 0; count--)
        25     {
        26          if (topNode == (NodeStruct *)NULL)
        27          {
        28             topNode = (NodeStruct *)malloc(sizeof(NodeStruct));
        29             lastNode = topNode;
        30
        31             if (lastNode == (NodeStruct *)NULL)
        32                  return; /*ERROR */
        33          }
        34          else
        35          {
        36             lastNode -> next = (NodeStruct *)malloc(sizeof(NodeStruct));
        37             if (lastNode -> next == NULL)
        38                  return; /*ERROR */
        39          }
        40
        41          lastNode = lastNode -> next;
        42          lastNode -> funcPtr = FuncPtrArry[count - 1];
        43          lastNode -> next = (NodeStruct *)NULL;
        44     }  // end of for loop
        45  }
        46
        47  int main()
        48  {
        49     buildlist();
        50
        51     while(topNode)
        52     {
        53          (*(topNode->funcPtr)) ();
        54          topNode = topNode->next;
        55     }
        56  }

    Thanks in advance

    Phil
  • philuu12
    New Member
    • Oct 2011
    • 14

    #2
    Output of debugging session:
    ------------------------------------------
    Breakpoint 1, main () at linked_list_net .cc:51
    51 while(topNode)
    (gdb) n
    53 (*(topNode->funcPtr)) ();
    (gdb) n

    Program received signal SIGSEGV, Segmentation fault.
    0x00000000 in ?? ()
    (gdb)

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Line 42 you set lastNode = lastNode -> next; but lastNode -> next is only set in the else clause of the the if statement so on the first iteration when topNode is created this line causes an error.

      Move line 42 into the else clause of the if statement.

      Comment

      • philuu12
        New Member
        • Oct 2011
        • 14

        #4
        Originally posted by Banfa
        Line 42 you set lastNode = lastNode -> next; but lastNode -> next is only set in the else clause of the the if statement so on the first iteration when topNode is created this line causes an error.

        Move line 42 into the else clause of the if statement.
        Thank you, Banfa

        P.

        Comment

        Working...