Xiangdong <
xiangd...@gmail.com> wrote:
> I have the following code which was used to check whether the result
> of (a * b) is larger than the maximum integer value.
>
> ovf(int a, int b, int *r, int *v)
> {
> *r = a * b;
> if (b == 0 || a == *r / b)
> *v = 0;
> else
> *v = 1;
>
> }
This can't work.
> int main()
> {
> int words;
> int bytes;
> int overflow;
>
> //... whatever
> ovf(words, sizeof(int), &bytes, &overflow);
> if (overflow)
> printf("overflow");
>
> }
>
> It doesn't work with GCC 4.5.2 if enabling -O2 or -O3, asm code
> generated by GCC is as follows, call to ovf is eliminated, seems
> that GCC evaluates (a == *r/b) to TRUE at compile time, is there
> anyway to revise ovf to let it satisfy GCC? thanks in advance.
Integer overflow is undefined, so GCC assumes that it doesn't happen.
Use unsigned ints instead of ints and you'll be fine.
Andrew.