Division by zero

Robert Dewar dewar@adacore.com
Sat Feb 10 13:09:00 GMT 2007


Ian Lance Taylor wrote:
> "Jie Zhang" <jzhang918@gmail.com> writes:
> 
>> But now gcc seems to optimize it away. For the following function:
>>
>> $ cat t.c
>> #include <limits.h>
>> void foo (int rc)
>> {
>>   int x = rc / INT_MAX;
>>   x = 4 / x;
>> }
> 
> I believe we still keep division by zero in general.  In your example
> it gets optimized away because it is dead code.  Nothing uses x.

And it is certainly reasonable to do this optimization given that
the result of the division is undefined in C. In Ada, such a
division has well defined semantics (raise an exception), but
it is interesting to note that the optimization is valid in
Ada as well, since there is a special rule that basically says
you don't need to evaluate an expression if the only reason for
doing so is to see if it raises a predefined exception. That
rule is precisely to deal with cases like this.
> 
> Ian



More information about the Gcc mailing list