C structs & A question about octet

Lawrence W. McVoy mcvoy at rsch.WISC.EDU
Wed Nov 5 09:16:27 AEST 1986


I have a question about alignment and padding.  I have noticed (context:
Vax 780, 4.3BSD) that the c compiler pads out struct sizes to be
long word aligned.  And it does the pointer arithmetic based on the
padded sizes. (no sh*t, sherlock, one would hope that they are the same)
For instance,

    typedef struct {
	    char	byte;
	    short	word;
    } three_bytes;

    sizeof(three_bytes) == 4, not 3.

    three_bytes* p = 100;

    p == 100, p+1 == 104, not 103.

For all of you that knew this, you're all saying big deal, so what?  Well,
I do (did) stuff like this all the time:

    head = (three_bytes*)calloc(N, sizeof(three_bytes));

This wastes N bytes.  Sometimes N is around 10 to the 7th or 8th.  Bad news.
The fact that pointer arith is "wrong" makes this very icky to work around
even if you are aware of the problem.  Anyone have any comments or
suggestions?  Does everyone except me know about this?

Also, what's this about alignment that I hear all the time?  If compilers are
already aligning things for you, why bother to do it explicitly?  You might
say "so it works on stupid compilers" but who are you to say what alignment
should be?  I mean, if you port code to a machine with 24 bit alignment and
you've carefully aligned all your stuff to 32 bit boundries, you've screwed
yourself.  No fun.  Also no gain.

OK, next question:  I want to define some types to hold bytes, words, and
long words, where byte == 8 undsigned bits, word == 16 unsigned bits, and
long words == 32 unsigned bits.  I want to give them nice names, names
that imply the number of bits.  I could use u8, u16, and u32, but I
don't *like* those names.  I thought I had a better plan:
	use octet for the byte
	use hexdectet for sixteen
	use <latin for two>, latin for 30> for 32
but 32 turned out to be "duotrentet" or something and that's ugly.  So
does anyone have any better names?  Something nice and intuitive and
not ugly?  How about Greek?  How do they spell them?
-- 
Larry McVoy 	        mcvoy at rsch.wisc.edu, 
      		        {seismo, topaz, harvard, ihnp4, etc}!uwvax!mcvoy

"They're coming soon!  Quad-stated guru-gates!"



More information about the Comp.lang.c mailing list