Thread: While loop in insertNode function is an infinite loop

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    14

    While loop in insertNode function is an infinite loop

    My program is supposed to read in each word from an input file and add it to a linked list, but it crashes after I enter the user input because the insertNode function/method is an infinite loop.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    struct node
    {
      char *data;
      struct node *next;
    };
    
    
    
    
    void insertNode(struct node**, char *);
    void printList(struct node*);
    
    
    
    
    int main()
    {
      struct node *head = NULL;
      FILE *fptr;
      char file_name[20];
      char str[1000];
      int numOfChar;
    
    
    
    
      printf("Enter the name of the file: ");
      scanf("%s",file_name);
    
    
    
    
      printf("Enter the number of characters per line: ");
      scanf("%d",&numOfChar);
    
    
    
    
      fptr=fopen(file_name,"r");
    
    
      while(fscanf(fptr, "%s ", str) != EOF)
      {
          insertNode(&head, str);
      }
    
    
      fclose(fptr);
      printList(head);
    
    
    
    
      return 0;
    }
    
    
    
    
    void insertNode(struct node** nodeHead, char *data)
    {
        struct node* new_node = malloc(sizeof *new_node);
        new_node->data = strdup(data);
        new_node->next = NULL;
    
    
    
    
    
    
        while (*nodeHead)
            nodeHead = &(*nodeHead)->next;
        *nodeHead = new_node;
    
    
    }
    
    
    void printList(struct node* node)
    {
        while(node != NULL)
        {
            printf(" %s ", node->data);
            node = node->next;
        }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You should probably learn what dereferencing really does. You are using it when you don't really need to.

    One thing that concerns me about the method you've applied is what happens to nodeHead.

    The first time this runs is fine. *nodeHead is a NULL pointer, so the loop is skipped and you go down to line 86. But on subsequent calls, this happens.
    Code:
    85              nodeHead = &(*nodeHead)->next;
    Not really what you want. Once nodeHead actually becomes NULL, you've lost the list, there's no way to get it back.

    I recommend using a spare pointer and looking for the node just before the end. That should work, because normally in linked list code, what you are really doing to it is updating links in other, previously existing nodes.
    Code:
    void insertNode(struct node** nodeHead, char *data)
    {
        struct node* new_node = malloc(sizeof *new_node);
        new_node->data = strdup(data);
        new_node->next = NULL;
    
        if (*nodeHead == NULL)
        {
            *nodeHead = new_node;
             return;
        }
        
        struct node* tail;
        for (tail = *nodeHead; tail->next != NULL; tail = tail->next)
            /* intentionally blank */ ;
        
        tail->next = new_node;
    }
    Light testing has shown that this version does work.
    Last edited by whiteflags; 03-19-2016 at 08:44 PM.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    ok thanks for your help. I tried your method but my program still crashes. I get a segmentation fault on line 14(in the code you posted) where the for loop is.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Like I said, it worked on my machine as-is.

    Maybe malloc() did not succeed? Normally, calls to malloc have their return values checked.
    Code:
    struct node* new_node = malloc(sizeof *new_node);
    if (new_node == NULL)
    {
        fprintf(stderr, "insertNode() malloc failed!\n");
        return;
    }
    ...
    See if that is the problem. If not, then the problem should definitely be elsewhere.

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    Yea that still did not work. The debugger is still saying there is a segmentation fault at the for loop and I have no idea why.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Did you compile without warnings? What are you using to build programs?

    I ask because some compiler vendors do not supply strdup(), and that is another potential problem that can cause a segfault.

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    I am using the Code::Blocks IDE. I did compile without warnings.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I don't know how to help you then. I did debug my function before posting it, and even in your existing code, it worked on my machine. I'm out of ideas.

  9. #9
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    ok thanks for your help. I have no idea either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. Can you tell me why this causes an infinite loop?
    By User Name: in forum C Programming
    Replies: 23
    Last Post: 07-10-2010, 12:21 AM
  3. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  4. Cant get out of infinite while loop
    By elusive in forum C++ Programming
    Replies: 12
    Last Post: 04-13-2005, 10:34 AM
  5. Infinite Loop!!!
    By catmom in forum C++ Programming
    Replies: 9
    Last Post: 12-11-2001, 10:44 AM