Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site harvard.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!godot!harvard!breuel From: breuel@harvard.ARPA (Thomas M. Breuel) Newsgroups: net.lang.c Subject: Re: casts and puns... Message-ID: <151@harvard.ARPA> Date: Wed, 14-Nov-84 14:03:29 EST Article-I.D.: harvard.151 Posted: Wed Nov 14 14:03:29 1984 Date-Received: Sat, 17-Nov-84 04:19:41 EST Distribution: net Organization: Harvard University Lines: 47 |>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@harvard)