Hello! # g++-7 --version g++-7 (Ubuntu 7.2.0-8ubuntu3.1~16.04.york0) 7.2.0 # g++-8 --version g++-8 (Ubuntu 8-20180218-1ubuntu1~16.04.york0) 8.0.1 20180218 (experimental) [trunk revision 257787] c++ code <sigover.cpp>: #include <stdio.h> class C1 {}; int main(void) { C1 c1; int a = 0x7FFFFFFA; for(int i=0; i<10; i++) printf("%d\n", ++a); return 0; } g++-7[8] -O2 -S sigover.cpp the loop assembler code with error: .L2: addl $1, %ebx movl $.LC0, %esi movl $1, %edi movl %ebx, %edx xorl %eax, %eax call __printf_chk jmp .L2 <-- infinite loop .cfi_endproc .LFE30: g++-7[8] -O1 -S sigover.cpp the loop assembler code without error: .L2: addl $1, %ebx movl %ebx, %edx movl $.LC0, %esi movl $1, %edi movl $0, %eax call __printf_chk cmpl $-2147483644, %ebx <-- check the end of loop jne .L2 movl $0, %eax popq %rbx .cfi_def_cfa_offset 8 ret .cfi_endproc .LFE30: if comment 1st line in main: // C1 c1; then g++-7 -O2 correctly generates the code but g++-8 -O2 still makes an infinite loop
signed overflow is undefined behavior at runtime.
gcc 5.4.1 make correct code.
Does not matter, the behavior is undefined.
(In reply to Andrew Pinski from comment #1) > signed overflow is undefined behavior at runtime. for(int i=0; i<10; i++) this loop is correct.
(In reply to Dmitry Lesnikov from comment #4) > (In reply to Andrew Pinski from comment #1) > > signed overflow is undefined behavior at runtime. > > for(int i=0; i<10; i++) > > this loop is correct. But there is an overflow with the variable a when i is 6.
(In reply to Andrew Pinski from comment #5) > (In reply to Dmitry Lesnikov from comment #4) > > (In reply to Andrew Pinski from comment #1) > > > signed overflow is undefined behavior at runtime. > > > > for(int i=0; i<10; i++) > > > > this loop is correct. > > But there is an overflow with the variable a when i is 6. Yes, but code generation incorrect. It's not runtime yet.
(In reply to Andrew Pinski from comment #5) > (In reply to Dmitry Lesnikov from comment #4) > > (In reply to Andrew Pinski from comment #1) > > > signed overflow is undefined behavior at runtime. > > > > for(int i=0; i<10; i++) > > > > this loop is correct. > > But there is an overflow with the variable a when i is 6. Why does the line C1 c1; affect the behavior of the loop?
> Yes, but code generation incorrect. It's not runtime yet. The criterion is as follows: does the code invoke undefined behavior when executed in the abstract machine specified by the C standard? If so, then the compiler is allowed to generate object code that does anything, including playing some music or erasing the hard drive; here it generates an infinite loop and that's OK.