Sizes, alignments, and maxima (was: Contiguous Arrays)

Henry Spencer henry at utzoo.uucp
Sun Feb 26 11:22:43 AEST 1989


In article <1839 at valhalla.ee.rochester.edu> badri at valhalla.ee.rochester.edu (Badri Lokanathan) writes:

>	unsigned i, j, k, val;
>
>	i = 100; j = 200; k = 300;
>
>	val = i - j + k;
>	printf("%u\n", val);
>
>	val = (i - j) + k;
>	printf("%u\n", val);
>
>	val = i - j;
>	printf("%u\n", val);
>}
>
>The output was
>200
>200
>4294967196
>
>Thus even though the intermediate value was rubbish (-100), it still
>worked correctly. (I checked the assembler output for difference too.)
>Could anybody tell me of a machine where this will not run?

No ANSI-conforming implementation will refuse to run this.  The intermediate
result was neither rubbish nor -100, it was 4294967196, which is perfectly
correct, legal, and standard-compliant.  Arithmetic on unsigned numbers is
defined to wrap around rather than causing overflow.

If you tried the equivalent with signed arithmetic, on some machines
(I believe the MIPS machines are among them), you would get overflow traps.

I can't immediately think of any currently-extant machines where the same
would happen with pointers, but that's no guarantee that there never will
be one.
-- 
The Earth is our mother;       |     Henry Spencer at U of Toronto Zoology
our nine months are up.        | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list