casts and puns (and digressions...)

Kevin Martin kpmartin at watmath.UUCP
Sun Nov 18 06:33:37 AEST 1984


In article <210 at desint.UUCP> geoff at desint.UUCP (Geoff Kuenning) writes:
>In article <940 at opus.UUCP> rcd at opus.UUCP (Dick Dunn) writes:
>
>>However, (long*)i is a pun; you just have to stretch
>>too far to make any sensible semantics which makes it a true cast.
>
>Oh yeah?  How about non-byte-addressable machines?  A C compiler for the DG
>Nova, for example, would have to generate a shift-right instruction for that
>cast.  Sounds like a conversion to me.

   (Throw glass of cold water in person's face, grab then by the shoulders
and shake them, slap them in the face a few times, look them squarely in
the face, and, for the n+1st time, say...)

THERE IS NO RULE THAT SAYS THAT (long *)i MUST POINT TO THE i'TH long
OR TO THE i'TH BYTE
OR TO THE i'TH ANYTHING.

It just happens to do this on pdp11's, vaxes, 68000's, 8086's, etc.

On a NOVA, (int *)1 could just as well point at word 1, and (char *)1
as the lower byte of word 1, and (char *)0100001 at the upper byte of
word 1, etc. In which case the cast really is a pun.

If a C compiler for the NOVA does a shift for int->pointer conversions,
it is only through the kindness of heart (and/or lack of brains) on the part
of the compiler developers. It is tough enough making sure zero turns into
the machine's null pointer without having to make every integer point to
the corresponding byte too.
                   Kevin Martin, UofW Software Development Group.



More information about the Comp.lang.c mailing list