[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Please don't do this (code fragment)

Manoj Srivastava writes:

> ... I see no references in the standard that require a particular
> behaviour on integer overflow, so I guess the behaviour is undefined
> by omission (though particular implementations may chose to have the
> behaviour well defined, if they wish).

Undefined by omission was also my conclusion.  As far as I know, the
standard is written like this to allow for ones-complement
implementations of signed integer, as well as for trapping on

For the conversion of signed int to unsigned int and back, the
specification is such that one a twos-complement machine you can just
reinterpret the bit pattern.

Going back to the original code, the assumption that

        int i;
        for (i = 0; i > 0; ++i)

will terminate, and will terminate within a reasonable amount of time,
is not warranted.  The program _may_ crash when i overflows, and the
int type may be large enough that the program effectively hangs.

Olaf Weber

               (This space left blank for technical reasons.)

Reply to: