When is a cast not a cast?

carroll at s.cs.uiuc.edu carroll at s.cs.uiuc.edu
Sat May 20 00:14:00 AEST 1989


/* Written 11:19 am  May 18, 1989 by gsh7w at astsun1.acc.Virginia.EDU in s.cs.uiuc.edu:comp.lang.c */
#In article <334.nlhp3 at oracle.nl> bengsig at oracle.nl (Bjorn Engsig) writes:
##[ pointer overflow example ]
#Then why can't one say "Integer addition IS meaningless. Let's take an
#example of a 16 bit machine. Let's also declare
#
#int i,j;
#
#and let i == 32000 and q == 32767. Then what is I+j? Which type does
#it have? The presumed answer of `64767' cannot be stored in this
#machine. 
#
/* End of text from s.cs.uiuc.edu:comp.lang.c */

Ah, but that's an entirely different case. The programmer has complete
control over what's in those int variables, and can do overflow checking
before hand, or just use unsigned arithmetic where the result is defined
(it is mod 2^(sizeof(int)*bits_per_byte)). The programmer, however, has
_no_ control over where the pointers are, or what the actual values in
them are. He could prevent the case you talk about for int's, but how would
you do it for pointers? You may think "only in contrived examples", but
on a 3b2, all user space pointers are such that adding them _will_ cause
overflow (static/malloc are 0x8000000, stack 0xC000000). I can write portable
code that prevents int overflow. I can't do that for pointers.

Alan M. Carroll                "And there you are
carroll at s.cs.uiuc.edu           Saying 'We have the Moon, so now the Stars...'"
CS Grad / U of Ill @ Urbana    ...{ucbvax,pur-ee,convex}!s.cs.uiuc.edu!carroll



More information about the Comp.lang.c mailing list