Un-alignment in structures

Marcus Hall thoth at tellab2.UUCP
Thu Mar 21 13:15:09 AEST 1985


In article <120 at mit-athena.UUCP> jc at mit-athena.UUCP (John Chambers) writes:
>If the data is unaligned (...) someone has to write the inefficient code to
>extract the data.  It is either me or the compiler.  This is not a difficult
>job for a compiler to do.  I know, I've written several.  
>
>Handling
>misaligned data is an especially dreary piece of drudge work that is both 
>hard for me and easy for the machine.
>

OK, how does the compiler know at compile time whether a pointer will be
pointing to an aligned structure or not?  It would have to assume that is
never is aligned, which would produce needlessly innefficient code 95% of
the time.  Consider

x(p)
int *p;
{
return (*p);
}

On the Z8000 which cannot do un-aligned word addressing, the following
code would be produced:

Alignment assumed:	Alignment of stack	No alignment assumed:
			only assumed:

ld	r5,2(sp)	ld	r5,2(sp)	ldb	rh5,2(sp)
ld	r4, at r5		ldb	rh4, at r5		ldb	rl5,3(sp)
ret			ldb	rl4,1(r5)	ldb	rh4, at r5
			ret			ldb	rl4,1(r5)
						ret

Now, it isn't all that terrible to assume alignment, is it?

Actually, as has already been stated, the biggest problem to portability
is byte ordering, so if you have to worry about alignment problems you
must also worry about byte ordering, so you've got to put the bytes together
yourself anyhow.

Of course, you could load the int into a register, then asm() in a swab
instruction :-).

marcus hall
..!ihnp4!tellab1!tellab2!thoth



More information about the Comp.lang.c mailing list