casts and puns...

Thomas M. Breuel breuel at harvard.ARPA
Thu Nov 15 05:03:29 AEST 1984


|>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.  The same goes for (long) i on most
|machines;  you have to sign-extend it.
|
|If I may leap to an unjustified generalization on insufficient data, I wonder
|if there is such a thing as a type pun at all, at least in a machine-
|independent sense.  On a Vax, for example, (int) i is a pun if i is a long,
|but on a PDP-11 it is a truncating conversion (cast).  On most machines a
|struct foo * is the same as a struct bar *, but the language doesn't require
|it (though if they are different sizes I don't envy the compiler writer).
|One can conceive of a machine where every structure had to be aligned on a
|structure-size boundary;  it is then reasonable to shift pointers right an
|appropriate number of bits.  (This makes the ++ operator easy to implement,
|and can be pretty cheap if the machine has built-in left-shifting as part of
|the indexing process, similar to the Vax's limited indexing capability.)
|-- 
|
|	Geoff Kuenning
|	First Systems Corporation
|	...!ihnp4!trwrb!desint!geoff
|

Nice if you can make sense of pointer casts. In general that is not
possible, however. Assume, for example, that you are working on a 36
bit machine and that you are storing four 8bit characters per word. A
pointer to a character would then consist of the address of the word
plus two bits telling which character in the word is the one you are
pointing at. What sense would you make of assigning a 36bit word to 
contents of a character pointer into the middle of another 36bit word?
Would you fill up the unused top 4bits?

A related problem occurs on 68000's, where word operations have to be
word aligned in memory. If you have a pointer p to a character,
(short *)p = 0; will give you an addressing error half of the time.
Should the compiler have word-aligned the pointer in the cast, or should
you let the addressing error happen?

I don't think that pointer casts/puns with non-word pointers (where
word refers to the smallest addressable unit) work very well, and that
you can make any machine independent sense of it.

						Thomas.
						(breuel at harvard)



More information about the Comp.lang.c mailing list