Simon F Tea maker Moderator Veteran Supporter Sep 7, 2009 #41 rpg.314 said: And with code like that, Click to expand... .. you deserve to be fired?
A arjan de lumens Veteran Sep 7, 2009 #42 rpg.314 said: And with code like that, if you put a printf("%d\n",i) inside the loop and run it thru the optimizer, you'll prolly wonder why the hell am I not getting an infinite loop? Click to expand... Actually, I did try, and I did get an infinite loop even with printf() in it. Input code, in C: Code: #include <stdio.h> int main(void) { int i; for(i=0;i<100;i--) { printf("%d\n",i); } } Assembly code generated by gcc-4.3.3 for the main() function, compiled with optimizations (gcc command line switch -O3): Code: globl main .type main, @function main: .LFB24: pushq %rbx .LCFI0: xorl %ebx, %ebx .p2align 4,,10 .p2align 3 .L2: movl %ebx, %edx movl $.LC0, %esi movl $1, %edi xorl %eax, %eax subl $1, %ebx call __printf_chk jmp .L2 Excerpt of output from actually running the program: Code: -2147483645 -2147483646 -2147483647 -2147483648 2147483647 2147483646 2147483645 2147483644 However, if I turn optimizations off (by giving gcc the commandline switch -O0 instead of -O3), then the loop is no longer infinite.
rpg.314 said: And with code like that, if you put a printf("%d\n",i) inside the loop and run it thru the optimizer, you'll prolly wonder why the hell am I not getting an infinite loop? Click to expand... Actually, I did try, and I did get an infinite loop even with printf() in it. Input code, in C: Code: #include <stdio.h> int main(void) { int i; for(i=0;i<100;i--) { printf("%d\n",i); } } Assembly code generated by gcc-4.3.3 for the main() function, compiled with optimizations (gcc command line switch -O3): Code: globl main .type main, @function main: .LFB24: pushq %rbx .LCFI0: xorl %ebx, %ebx .p2align 4,,10 .p2align 3 .L2: movl %ebx, %edx movl $.LC0, %esi movl $1, %edi xorl %eax, %eax subl $1, %ebx call __printf_chk jmp .L2 Excerpt of output from actually running the program: Code: -2147483645 -2147483646 -2147483647 -2147483648 2147483647 2147483646 2147483645 2147483644 However, if I turn optimizations off (by giving gcc the commandline switch -O0 instead of -O3), then the loop is no longer infinite.