On Sat, Sep 21, 2013 at 07:53:01PM +0200, Marc Glisse wrote: > On Sat, 21 Sep 2013, Jonathan Wakely wrote: > > >On 21 September 2013 17:46, wrote: > >>Is it possible for gcc to produce a > >>warning in such situation? > > > >If ab and bc are 'const' then G++ will warn: > > > >o.cc: In function ‘int main()’: > >o.cc:5:14: warning: integer overflow in expression [-Woverflow] > >int r = ab * bc; > > ^ > > > >The C compiler still doesn't warn though, due to different rules for > >how constants are handled between C and C++. > > It does warn with -O though... (still only with const) > > That's just the compiler not being good enough. I was going to > mention -fsanitize=undefined for gcc-4.9 for a runtime warning, but > it doesn't handle this yet, and -ftrapv never really worked. It tried -O2 and it also worked. However on my system it only said: warning: integer overflow in expression How did you get gcc to produce this part: [-Woverflow] I use gcc 4.5.2. But when I use gcc like this I also do not get any warnings: gcc -Woverflow main.c -o main I wonder: 1) why -Woverflow appears in your gcc output? 2) why `gcc -Woverflow main.c -o main' does not produce a warning? 3) why does `gcc -O main.c -o main' produce warning in the first place and why does it do this only when two operands are const? -- <wempwer@xxxxxxxxx>