On Tue, Nov 15, 2005 at 02:15:44PM -0700, Jeffrey A Law wrote: > > So, is it just me or does execute/930529-1.c invoke undefined or > implementation defined behavior due to its reliance upon overflow > behavior for signed types? > > In particular look at the control for the second loop: > > int i; > [ ... ] > > for (i = ((unsigned) ~0 >> 1) - 3; i <= ((unsigned) ~0 >> 1) + 3; i++) > > > i <= ((unsigned)~0>>1) + 3 > > Seems like it overflows to me, or would cause "i" to have to > overflow to terminate the loop.
There is no overflow in the test; the RHS is an unsigned expression (mix signed and unsigned, you get unsigned). This means that the termination test is equivalent to unsigned(i) <= ((unsigned)~0>>1)+3 or unsigned(i) <= (~0U>>1U)+3U However, the test does rely on the autoincrement wrapping around; specifically that i++ sets i to (int)(((unsigned)i)+1U). Perhaps the test could be rewritten as unsigned u; for (u = (~0U >> 1) - 3U; u <= (~0U >> 1) + 3U; u++) { int i = (int)u; ... }