-1

I am running this below 'C' program which has an infinite loop. I see the CPU reaching almost 99% using the 'top' command. Then suddenly the program stops. I was curious to know what rule in the system is causing it to stop.

I am running this under a docker image alpine:3.1 (+ some modules) and I just simply start its ash (alpine's bash).
Also, I can not find ulimit.

#include <stdio.h>
int main() {
    int marks[10],i,n,sum=0;
    printf("Enter number of students: ");
    scanf("%d",&n);
    for(i = 0; i < n; --i) {
        //printf("Enter marks of student%d: ",i+1);
        int c;
        c = 5;
        int a;
        a = c;
    }
    return 0;
}
6
  • what rule causes infinite loop program to stop a 'C' program ?
    – j10
    Jun 14, 2016 at 11:16
  • probably n has a garbage value, since you did not check the result from scanf. That could be anything, causing an indefinite loop in the program. Jun 14, 2016 at 11:16
  • 1
    Your code invokeds undefined behaviour by signed integer over/underflow. Anything can happen. Jun 14, 2016 at 12:22
  • 3
    Why do you think that loop that demonstrably stops is "infinite"? ;) Jun 14, 2016 at 12:34
  • @Aconcagua: Rolled back your edit, because you changed the code. Jun 14, 2016 at 12:36

2 Answers 2

12

As you mention, you could expect this to produce an infinite loop, at least if n is positive.

The behaviour you're seeing instead is caused by your counter wrapping around: when it reaches INT_MIN, the result of --i is INT_MAX (at least, with GCC's default settings on Intel-style CPUs — this is not defined in C, see below) and i<n fails.

So your program uses 100% of one CPU while it counts down from 0 to INT_MIN, then wraps around to INT_MAX and stops.

You can add

printf("%d\n", i);

just before the return line to see the value of i when the loop exits.

Note that this is undefined behaviour in C... If you compile this with -O2, at least with GCC 5.3, the program never exits the loop, because the optimiser decides that the exit condition will never be reached, and compiles the loop as a tight infinite loop if n is positive (.L4: jmp .L4).

2
3

Your loop is not infinite per se. YOu have a termnation condition which can be reached - in principle. The CPU load occurs, just because the loop runs for (likely) >2000000000 iterations before it ends.

First of all, you don't check the result of scanf. If it failed, n is unspecified.

Worse, your code invokes undefined behaviour (UB) due to signed integer over-/underflow. You decrement i past the minimum representable int value INT_MIN. Note that C does not have arbitrary length integers like Python.

Undefined behaviour means anything can happen, all bets are off. In your case, it might wrap to the max. positive int INT_MAX, thus the comparison fails and the loop is exited. But this is nothing you can rely on!

From the text and rest of the code, you likely don't want to decrement i, but increment: i++, so it counts from 0 to n - 1. Note that you also should check n for a valid range and ask the use to enter a valid value again. For the example, a negative upper limit is useless (but it does also no harm).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.