This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: how to make gcc warn about arithmetic signed overflow


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]