Thread: Why is this an infinite loop?

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    3

    Why is this an infinite loop?

    Code:
    struct node{
    struct node* back;
    int data;
    struct node* next;
    };
    struct node *head;
    struct node *tail;
    struct node *temp;
    
    void main{
    head = (struct node*)malloc(sizeof(struct node));
    tail = (struct node*)malloc(sizeof(struct node));
    tail->back = head;
    head->next = tail;
    temp = tail;
    while(temp->back != head){
    temp = temp->back;
    }
    }
    I guess I just don't understand at all as to why this loops infinitely. The intentions of this code is for temp to become the node "in front of the head node". How would I fix this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Caleb Duncan
    I guess I just don't understand at all as to why this loops infinitely.
    It should not loop infinitely: you set tail->back to head, then you compare temp->back to head, where temp == tail. Hence the condition is false even before the first iteration of the loop. Consequently, either you are misinterpreting your output, or the code that you posted is not what you actually compiled and ran (and indeed it is not so because the code that you posted contains a syntax error).

    Here's proof:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    struct node {
        struct node* back;
        int data;
        struct node* next;
    };
    
    struct node *head;
    struct node *tail;
    struct node *temp;
     
    int main(void) {
        head = (struct node*)malloc(sizeof(struct node));
        tail = (struct node*)malloc(sizeof(struct node));
        tail->back = head;
        head->next = tail;
        temp = tail;
        while (temp->back != head) {
            temp = temp->back;
        }
    
        printf("Hello world!\n");
        return 0;
    }
    The above prints "Hello world" for me, clearly showing that there is no infinite loop.

    A few comments:
    • You desperately need to indent your code properly.
    • void main should be int main(void) as in my code example.
    • Avoid global variables: head, tail and temp should have been declared in main.
    • There is no need to cast the return value of malloc. I recommend:
      Code:
      head = malloc(sizeof(*head));
    • Check that malloc does not return a null pointer before using the pointer.
    • free what you malloc when done.


    Quote Originally Posted by Caleb Duncan
    The intentions of this code is for temp to become the node "in front of the head node". How would I fix this?
    temp is tail though. You basically want to move the tail to be the head? Or are you trying to insert a new node as the head?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop in insertNode function is an infinite loop
    By blongnec in forum C Programming
    Replies: 8
    Last Post: 03-19-2016, 09:57 PM
  2. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  3. infinite loop
    By ssjnamek in forum C++ Programming
    Replies: 11
    Last Post: 09-25-2005, 05:08 PM
  4. 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
  5. Infinite loop?
    By ER in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2001, 05:47 PM

Tags for this Thread