Division by zero

Bernd Jendrissek berndj@prism.co.za
Mon Feb 12 09:38:00 GMT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sat, Feb 10, 2007 at 03:09:41PM +0200, Robert Dewar wrote:
> 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.

Right.  Sometimes it's tricky to remember that NOT reciting Hamlet's
soliloquy also qualifies as "undefined behaviour"! :)

> And it is certainly reasonable to do this optimization given that
> the result of the division is undefined in C.

There's nothing wrong with foo(INT_MAX).  Or variants involving INT_MIN.

- -- 
A PC without Windows is like ice cream without ketchup.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFF0DEcwyMv24BBd/gRAqkXAJ90Oi+pgZAWwE9nwu7HIA/AbYF32QCfaurK
bqj67tA92Uqcwj+jfvUrQTQ=
=WBmH
-----END PGP SIGNATURE-----



More information about the Gcc mailing list