This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: how to make gcc warn about arithmetic signed overflow
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: wempwer at gmail dot com
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Sat, 21 Sep 2013 19:27:20 +0100
- Subject: Re: how to make gcc warn about arithmetic signed overflow
- Authentication-results: sourceware.org; auth=none
- References: <20130921164609 dot GC3086 at a dot lan> <CAH6eHdTToM+TMy55m5HYo39DC8nA0RrTma1Bp5OnhUtPErMfOA at mail dot gmail dot com> <alpine dot DEB dot 2 dot 10 dot 1309211947030 dot 3549 at laptop-mg dot saclay dot inria dot fr> <20130921180952 dot GF3086 at a dot lan>
On 21 September 2013 19:09, <wempwer@gmail.com> wrote:
>
> 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
4.5.2 is quite old now. More recent versions show the warning option
that triggered the warning, that's what the [-Woverflow] part is.
> I wonder:
>
> 1) why -Woverflow appears in your gcc output?
Because it's a recent version.
> 2) why `gcc -Woverflow main.c -o main' does not produce a warning?
Because without optimisation the compiler doesn't do the necessary
analysis to realise that in the multiplication expression it knows the
values.
> 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?
Because the optimiser can track the values of constants (because they
don't change) and see that the values in the multiplication are known.