Byte order (retitled)

guido at boring.UUCP guido at boring.UUCP
Mon Apr 21 03:16:34 AEST 1986


In article <1104 at psivax.UUCP> friesen at psivax.UUCP (Stanley Friesen) writes:
>	Could you expand on this! Do you mean that if you cast a
>pointer to a long to a pointer to a short and dereference you will get
>the *high* order portion on a big-endian machine and the *low* order
>portion on a little-endian? Clearly a portability problem, and the the
>big-endian behavior is counter-intuitive.

You're right.  Casting "pointer to long" to "pointer to short" in C has
a portability problem.  This can't be solved by outlawing big-endian
architectures, nor by 'fixing' the C compilers.  Non-portable casts have
have an effect that is obvious or at least understandable when you know
the underlying hardware architecture of a given implementation; never
mind intuitivity, and you correctly describe the effect of this
particular example.

If you want to write portable C, don't use non-portable constructs.
If you want to flame big-endianness, don't use C portability as an
argument.

You're right about the counter-intuitivity of the effect of
dereferencing a wrong-size pointer in assembly language, and (as has
been said numerous times by now) this was one of the resons for its
use in the PDP-11.

-- 
	Guido van Rossum, CWI, Amsterdam <guido at mcvax.UUCP>



More information about the Comp.lang.c mailing list