When implementing an infinite loop, is there a difference in using while (1) vs for (;;) vs goto (in C)?

When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto ?

Thanks Chenz

+20
c loops for-loop while-loop goto
Feb 18 2018-10-18
source share
8 answers

They are equivalent even if you turned off the optimizer.

Example:

 #include <stdio.h> extern void f(void) { while(1) { putchar(' '); } } extern void g(void) { for(;;){ putchar(' '); } } extern void h(void) { z: putchar(' '); goto z; } 

Compiling with gcc -O0 gives an equivalent build for all three functions:

  f: ; [ EXTERNAL ] ; +00000 00000fb4 80402DE9 stmdb sp!,{r7,lr} +00004 00000fb8 00708DE2 add r7,sp,#0x0 +00008 00000fbc 2000A0E3 loc_000008: mov r0,#0x20 +0000c 00000fc0 0A0000EB bl putchar (stub) +00010 00000fc4 FCFFFFEA b loc_000008 ; ; g: ; [ EXTERNAL ] ; +00000 00000fc8 80402DE9 stmdb sp!,{r7,lr} +00004 00000fcc 00708DE2 add r7,sp,#0x0 +00008 00000fd0 2000A0E3 loc_000008: mov r0,#0x20 +0000c 00000fd4 050000EB bl putchar (stub) +00010 00000fd8 FCFFFFEA b loc_000008 ; ; h: ; [ EXTERNAL ] ; +00000 00000fdc 80402DE9 stmdb sp!,{r7,lr} +00004 00000fe0 00708DE2 add r7,sp,#0x0 +00008 00000fe4 2000A0E3 loc_000008: mov r0,#0x20 +0000c 00000fe8 000000EB bl putchar (stub) +00010 00000fec FCFFFFEA b loc_000008 
+44
Feb 18 2018-10-18
source share

I just compared the unoptimized gcc assembler output:

 # cat while.c int main() { while(1) {}; return 0; } # cat forloop.c int main() { for (;;) { }; return 0; } 

Draw the assembler output:

 # gcc -S while.c # gcc -S forloop.c 

Compare assembler files:

 # diff forloop.s while.s 1c1 < .file "forloop.c" --- > .file "while.c" 

As you can see, there are no significant differences. Here is the conclusion

 # cat while.s .file "while.c" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp .L2: jmp .L2 # this is the loop in both cases .size main, .-main .ident "GCC: (GNU) 4.4.3" .section .note.GNU-stack,"",@progbits 

Although this is not technical evidence that they are the same, I would say that it is in 99.9% of cases.

+6
Feb 18 2018-10-18
source share

There is hardly any difference in the generated assembly. This is rather a stylistic problem:

Jump in - just oooh: jumps back without a clear endless block

while (1) - better, the condition is "dummy", although you will often be warned by the compiler (warning level 4) or a static analysis tool

for (;;) may not be the most beautiful, but imho is best suited because this construct cannot have any other meaning (compared to that time). But some other people still prefer (1) for the "same" reason ...

+4
Feb 18 '10 at 13:36
source share

Absent. Use what is most readable to you

+3
Feb 18 2018-10-18
source share

while(1) and for(;;) are exactly equivalent, and both are well-understood idioms for coding infinite loops.

I would avoid using goto : to break out of an infinite loop or move on to the next iteration, use break and continue .

+3
Feb 18 2018-10-18
source share

In C, true is implemented as follows (depending on the compiler)

 #define TRUE 1 

or

 #define TRUE (-1) 

And false is executed as

 #define FALSE 0 

therefore, while (1) equivalent to while (true) , since 0 is considered false.

the while (1) == for (; ;) , since there are no stop conditions.

which translates to assembler as

 :loop ... ... ... goto loop 

therefore, if the assembler code does not have a ret or exit statement, it is considered an infinite loop.

+2
Feb 18 2018-10-18
source share

Despite the lack of significant differences, as mentioned in other posts, the common reason for using for (;;) instead of while (1) is that static analysis tools (and some compilers with certain warning levels) often complain about the while loop.

Goto is a little nasty, but should contain the same code as the others. Personally, I stick with for (;;) (to keep Lint happy), but I have no problem with while (1) .

+2
Feb 18 2018-10-18
source share

From what I recall about β€œsorting out the years,” it will not matter much (compilers are smart enough). This is more about IMO aesthetics.

0
Feb 18 2018-10-18
source share



All Articles