0
Subscription *current_sub;
current_sub = sub_user->subscriptions;
while (current_sub != NULL){
    Event *sub_events;
    sub_events = current_sub->calendar->events;
    while (sub_events != NULL){
        add_event(ordered_events, od, sub_events->description, sub_events->time);
        printf("added! \n");
        if(sub_events ->next != NULL){
        sub_events = sub_events->next;
        }
    }
    if (current_sub->next != NULL) {
        current_sub = current_sub->next;
    }
}

So my loop is infinitely looping for some reason and I can't figure out why. both checks for null, and my linked lists should all terminate at some point. Is there something having to do with double while loops checking null pointers that I should be aware about?

EDIT: never mind the infinite loop is fixed. Thanks so much!

5
  • Could it be that one of the lists is circular ? Plus (stylistic) consider using for() loops instead of while() , it will save 4 lines, and "continue" won't skip the "increment" part of the loops. Commented Oct 15, 2014 at 19:56
  • 3
    Please don't edit in place like that ... it invalidates the answers. And did you successfully recompile after making the changes and use the new version? Your printf should print more information that tells you which Events you are processing. And your list could be circular, or add_event could add the event back to the list you're scanning ... there's a lot of info missing here. Commented Oct 15, 2014 at 19:56
  • @JimBalter (and OP) I rolled it back. Indeed, the answers seem to make no sense if the if() s are removed. Commented Oct 15, 2014 at 20:02
  • sorry i actually didn't mean to change that. I copy pasted and i thought i editted a 2nd block of code but it just modified the first block. also i couldn't recompile it after using the 2nd version. I still get some kind of infinite loop Commented Oct 15, 2014 at 20:04
  • Now you have two copies of the same code. I suspect operator error is the cause of your problems. "also i couldn't recompile it after using the 2nd version" -- Eh? Why not? " I still get some kind of infinite loop" -- And you still haven't provided the sort of information needed to diagnose that. Commented Oct 15, 2014 at 20:14

2 Answers 2

1

You are checking for this condition

while (current_sub != NULL)

And then with an if condition, making sure that if only current_sub->next != NULL then increment current_sub

if (current_sub->next != NULL)

So, the current_sub is never incremented to next when the next points to NULL

Same is the case with the inside while (sub_events != NULL) and the if(sub_events ->next != NULL)

Sign up to request clarification or add additional context in comments.

2 Comments

so after I removed it, I'm still getting an infinite loop. is there another condition that I should be checking?
I understand that now, but after removing, I still have an infinite loop for some reason
0
if(sub_events ->next != NULL){
    sub_events = sub_events->next;
}

If sub_events ->next is NULL, sub_events won't change, so the next iteration will use the same value, ad infinitum. Same for

if (current_sub->next != NULL) {
    current_sub = current_sub->next;
}

The conditionals make no sense; just remove them and do the assignments unconditionally.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.