struct 32 bit aligned and padded

Ron Stanonik stanonik at nprdc.navy.mil
Fri Apr 12 02:23:46 AEST 1991


We ran into a little problem while porting some 4.3bsd/sunos
code to a 3b2 running sysVr3.2.  The problem was that on the
3b2, structs are not only aligned on 32 bit boundaries (which
we expected), but also padded to 32 bit boundaries.  For example,
here's a little program
	struct s0 { char c[2]; };

	struct s1 { struct s0 s0; short s; } x1;

	main()
	{
		printf("offset of short = %d\n", (int)&x1.s - (int)&x1);
	}

We expected the short x1.s to be two bytes from the start of the struct x1.
That's what happens on vax's running 4.3bsd, sun's running sunos4.1, hp's
running hpux, 386's running sysVr4.  But on the 3b2 running sysVr3.2,
x1.s is offset 4 bytes, apparently because the struct s0 is padded
out to a 32 bit boundary in s1.  Is this a "feature" of sysVr3.2,
or just the 3b2's?  From K&R second edition, I gather there are no
assurances regarding alignment or padding in this instance, but this
behaviour seems contrary to the usefulness of C as a low level language.

(Specfically we ran into this because EGP headers end on a 16 bit
boundary, immediately followed by the remainder of the EGP packet.
Because the header was padded, the remainder of the packet was
incorrectly positioned.)

Thanks,

Ron Stanonik
stanonik at nprdc.navy.mil
ucsd!nprdc!stanonik



More information about the Comp.unix.programmer mailing list