Un-alignment in structures

Rick Richardson rick at sesame.UUCP
Tue Mar 19 04:06:30 AEST 1985


> 	the reason for the padding, which admittedly does not hold for
> some machines, is that many machines, notably the pdp-11, can not
> handle words generated at an odd address.

The original author (sorry I don't have rn, and lost his name) wanted
unaligned structures for what looks like a very legitimate and portable
reason.  He's trying to package up a block of data which is to be
sent over a serial line.  Probably some protocol.  He would love to
give symbolic names to the pieces of data instead of trying to
pack the stuff by hand into a character array (using >>8 and &0xff, etc.).
His ultimate goal is to issue a "write(fd, &block, sizeof(block));".
This feature wouldn't cause any problem with word alignment, if used
in this manner.  The problem is that even if the padding is eliminated
with some language construct, he is still going to have machine
dependencies in two areas:  an assumption that "short" or "int" is
a specific length (probably 16 or 32 bits), and that the byte ordering
of such things is the same on his machine as specified by the protocol
and also of all machines to which he may port.  This is not the case,
and the only portable way to do this is by shifting and masking the
stuff into a character array.  The net is that he is specifically asking
for a language feature that will lull him into a sense of portability
that isn't really present.
-- 
		Rick Richardson, PC Research, Inc.

	{genrad|ihnp4|ima}!wjh12!talcott!sesame!{rick|pcrat!rick}
		{cbosgd|harvard}!talcott!sesame!{rick|pcrat!rick}
			rick%sesame at harvard.ARPA 



More information about the Comp.lang.c mailing list