-2

Just curious, why below code is not equivalent to forever loop? Instead application crashes.

unsigned short i;
for (i = 1; i >= 0; i++)
{
    printf("%d\n", i);
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Jon Wheelock
  • 263
  • 3
  • 16
  • 1
    No idea. I am getting an infinite loop, or at least as much of one as it seemed during the 5s I let it run. When do you get the crash? With what error message? What does `gdb` say about it? – Amadan Sep 28 '15 at 01:58
  • 5
    This code is correct (and causes infinite loop). Please post a [MCVE](http://stackoverflow.com/help/mcve). Your crash may be caused by something else in your program. Another possibility is that your terminal cannot handle the volume of output and the terminal crashes or appears to crash. – M.M Sep 28 '15 at 01:59
  • Obviously the code would crash since it would run an infinite loop! What is your question? – Rahul Jha Sep 28 '15 at 02:07
  • OK, now I see that it starts executing in endless loop after approx. 25 seconds. This is only during first time run. Subsequent runs are immediate and run endlessly. I am using latest codeblocks + mingw and not sure why this much delay is there. May be Kaspersky is the one which is causing problem. Even below code also runs after around 25 second { int k = 1; for (;k;) printf("Hello"); } – Jon Wheelock Sep 28 '15 at 02:12
  • 1
    Is this the whole code? Please post all the code that is causing the problem because the posted code will not cause a problem at all, it will just go from `1` to `65535` over and over again. – Iharob Al Asimi Sep 28 '15 at 02:37
  • Compile your actual code with all warnings & debug info (`gcc -Wall -Wextra -g` if using [GCC](http://gcc.gnu.org/)...). Then **use a debugger** (`gdb`) – Basile Starynkevitch Sep 28 '15 at 03:10
  • This is the whole code and the problem is Karsperky and not C related. I disabled Karspersky and it works. – Jon Wheelock Sep 28 '15 at 06:18

3 Answers3

1

The problem is solved. Antivirus Karspersky was analysing this exe file for nearly 20 seconds it looks. If I disable antivirus, it runs immediately. Thanks for all your inputs.

Jon Wheelock
  • 263
  • 3
  • 16
0
    unsigned short i;
for (i = 1; i >= 0; i++)
{
    printf("%d\n", i);
    system("pause");
}

Maybe it is so fast that it break , you can use it that make a pause but you need to press enter to keep the loop

Amadeu Antunes
  • 678
  • 1
  • 8
  • 28
-3

unsigned short i;

for (i = 1; i >= 0; i++) {

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

}

This loop will crash when value of i becomes bigger than a short can take.

Razack
  • 1,826
  • 2
  • 16
  • 37
  • 2
    I don't think that's true. When `i` reaches the largest possible value for `unsigned short`, the next increment will wrap it around to 0. See http://stackoverflow.com/questions/988588/is-using-unsigned-integer-overflow-good-practice. I believe this behavior is defined by the C Standard; if the compiler causes this code to crash, the compiler is broken. The bug is probably somewhere else in the code. – Nate Eldredge Sep 28 '15 at 04:50