C structure alignment - (nf)

John Crane crane at fortune.UUCP
Tue Apr 24 03:16:29 AEST 1984


	Many CPU's cannot reference an int at an odd-byte
	address, so their C-compiler ALWAYS locates a
	structure on an even-byte address.  On some
	machines (with 4-byte int's), the restriction is
	to 4-byte addresses.  This applies to structures
	within structures, even when the structures only
	contain char-arrays.

If you recall my question, I am not interested in getting ints; I
want chars.


	Note that the compilers are permitted to do this,
	because the only LEGAL things you can do to a
	structure are to take its address, reference a
	single member, or assign the entire structure to
	another structure of the same type (newer C-
	compilers only).  Internal padding can never be
	significant for these operations (see the recent
	discussion of == for structures).

All I legally wanted to do was refer to a single member which
happened to be a char.

	It is NOT PORTABLE to assume the ordering of
	members within a structure, nor to assume they are
	contiguous in memory.  Some machines that are not
	byte-addressable may have some big surprises here
	(the PDP-10 comes to mind).  For all byte-
	addressable machines with which I am familiar, if
	you make EVERY member of a structure a multiple of
	4 bytes long, data can be passed between machines
	in the format of the structure, ASSUMING THE BYTE-
	ORDERING IS THE SAME, OR HAS BEEN CORRECTED.

Portability schmortability! Does portability ALWAYS have to be an issue?
My question related to converting data from ONE SPECIFIC MACHINE to
ANOTHER SPECIFIC MACHINE. I did not make this clear in my original
question. Now listen. If I programmed for the DPD-10 and all its foibles,
or any other machine for that matter, that's not portable either. Maybe
there's a difference between portability and machine-independence.

I still put aligning nested structures that begin with chars in the
area of dirty tricks. Its one of the things I don't like about BASIC.
It assumes you don't know what you are doing and does what it "thinks"
is going to make life easier for you. One nice thing about assembly
language is that you can control your own alignment.

John Crane



More information about the Comp.lang.c mailing list