C Segfault help

This is a discussion on C Segfault help within the C Programming forums, part of the General Programming Boards category; Code: // // Declaration for linked list of nodes. // typedef char *cstring; typedef struct node *node_ref; struct node { ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    C Segfault help

    Code:
    //
    // Declaration for linked list of nodes.
    //
    typedef char *cstring;
    typedef struct node *node_ref;
    struct node {
       cstring string;
       node_ref link;
    };
    
    typedef struct list *list_ref;
    struct list {
       node_ref head;
    };
    
    int main (int argc, char **argv) 
    {
       char *progname = basename (argv[0]);
       list_ref list = NULL;
       int linenr = 0;
       char buffer[72];
       
       for (linenr = 1; ; ++linenr) 
       {
          char *nlpos = NULL;
          node_ref temp = NULL;
          
          printf("5");
          
          // Read a line of input and check to see if it ends with
          // a newline character.  Print a message if not.
    
          char *gotline = fgets (buffer, sizeof buffer, stdin);
          
          printf("7");
          if (gotline == NULL) break;
          printf("6");
    
          nlpos = strchr (buffer, '\n');
          if (nlpos != NULL)
             *nlpos = '\0';
          else 
          {
             fprintf (stderr, "%s: %d: unterminated line: %s\n",
                      progname, linenr, buffer);
          }
    
          printf("xyz");
          
          
          node_ref prev = NULL;
          
          node_ref curr = list->head;
    
          printf("2");
          // Find the insertion position.
          while (curr != NULL) 
          {
             if ( strcmp(buffer, curr->string) > 0) 
                break;
             prev = curr;
             curr = curr->link;
          }
          printf("3");
    
          // Do the insertion.
          temp = malloc (sizeof (struct node));
          temp->string = buffer;
          temp->link = curr;
          if (prev == NULL) 
             list->head = temp;
          else 
             prev->link = temp;
          
       }
    
       // Print the results in debug mode. Edit this so that only print if -d operand
    
       /*printf ("%s: head= %p\n", argv[0], head);
       while (head != NULL) {
          node_ref old = head;
          head = head->link;
          printf ("%s: %p-> node {\n"
                  "    string= %p->\"%s\",\n"
                  "    link= %p}\n",
                  progname, (void*) old, (void*) old->string,
                  old->string, (void*) old->link);
       };*/
    
       return EXIT_SUCCESS;
    }
    So I'm getting a seg fault somewhere and I can't figure out where. I've tried commenting it various sections of my code to no avail. I have an idea of why seg faults occur, but my understanding is limited.

    It is supposed to imitate this java program. I'm merely trying to convert the java code to C code.
    Code:
    import java.util.Scanner;
    import static java.lang.System.*;
    
    class sortlist 
    {
    
       static class list_t 
       {
          node_t head;
       }
       static class node_t 
       {
          String item;
          node_t link;
       }
    
       static void insertascending (list_t list, String newitem) 
       {
          node_t prev = null;
          node_t curr = list.head;
    
          // Find the insertion position.
          while (curr != null) 
          {
             if (curr.item.compareTo (newitem) > 0) 
                break;
             prev = curr;
             curr = curr.link;
          }
    
          // Do the insertion.
          node_t temp = new node_t();
          temp.item = newitem;
          temp.link = curr;
          if (prev == null) 
             list.head = temp;
          else 
             prev.link = temp;
       }
    
       public static void main (String[] args) 
       {
          Scanner stdin = new Scanner (System.in);
          list_t list = new list_t();
    
          while (stdin.hasNextLine()) 
          {
             String line = stdin.nextLine();
             insertascending (list, line);
          }
    
          for (node_t curr = list.head; curr != null; curr = curr.link) 
          {
             out.printf ("%s%n", curr.item);
          }
       }
    
    }

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Do you ever set 'list' to be anything other than NULL?

    If not, list->head is very dubious.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    Well what I want it to do is have list be null so that
    Code:
    node_ref curr = list->head;
    would be null. If curr is null, then the while loop just below it doesn't run.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,569
    This is NOT useful for debugging

    Code:
    printf("xyz");
    This is slightly more likely to work.
    Code:
    printf("xyz\n");
    I am guessing this is even more likely to work.
    Code:
    fprintf(stderr, "xyz\n");
    Tim S.

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Well, you can't do that.
    The following are equivalent:

    Code:
    list->head
    (*list).head
    If list is NULL, you are dereferencing NULL and you are not allowed to do that.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another SEGFAULT
    By cerr in forum C Programming
    Replies: 8
    Last Post: 01-14-2010, 10:04 AM
  2. Segfault
    By Triumph in forum C Programming
    Replies: 5
    Last Post: 04-10-2009, 02:24 AM
  3. segFault
    By Fox101 in forum C Programming
    Replies: 1
    Last Post: 04-10-2008, 12:00 AM
  4. Segfault
    By oddball in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 07:53 AM
  5. segfault with gcc, but not with TC
    By koodoo in forum C Programming
    Replies: 15
    Last Post: 04-23-2007, 09:08 AM

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