structure alignment question

notes at hcx1.UUCP notes at hcx1.UUCP
Sun Sep 14 07:15:33 AEST 1986


Background:  Our architecture has the following data types and sizes:

               byte   -- 1 byte
	       short  -- 2 bytes
	       int    -- 4 bytes
	       long   -- 4 bytes
	       *ptr   -- 4 bytes

Question:  Should structures and unions always be aligned to 4-byte
           boundaries?

Consider:   Our portable C compiler aligns this structure as follows:

     struct a
     {
        char b ;        ->  byte 0
        union aa
        {
           short bb;    ->  byte 4
           char cc;
        } dd ;
        char c;         ->  byte 8
     } d;

I've been told that on a VAX, however, 'bb' is placed on byte 2 and
'c' is placed on byte 4.  Is this true?  How do other compilers handle
this case?  Which way is "correct"?  If there is sufficient interest,
I will post a summary of responses.

Dave Ray  --  uucp:  {ucf-cs|allegra}!novavax!hrshcx!hcx1!daver



More information about the Comp.lang.c mailing list