Dalibor Topic wrote: > David Holmes - Sun Microsystems wrote: > > > In C, the result of an overflowing add of two signed integers is > > > undefined. > > > > Strewth! That's a surprise to me. I always thought that C defined > > integer arithmetic to always wrap. > > Only for unsigned operands (from 6.2.5 - Types): > > "A computation involving unsigned operands can never overflow, > because a result that cannot be represented by the resulting > unsigned integer type is reduced modulo the number that is one > greater than the largest value modulo reduced that can be > represented by the resulting type."
If I remember rightly the logic behind this is that having to worry about overflows makes a number of loop optimizations impossible. Most loop counters are signed, they made the result of signed overflows undefined while defining the result of unsigned overflow so that there was something you could use if you wanted to detect them. Cheers, Gary -- http://gbenson.net/