Size of structure containing char fields

Jim Patterson jimp at cognos.UUCP
Tue Jul 24 05:42:15 AEST 1990


In article <1030 at lzaz.ATT.COM> bds at lzaz.ATT.COM (Bruce Szablak) writes:
>Given a structure that contains only char fields (possibly unsigned):
>
>	struct example1 { char a, b, c; };
>
>is ANSI restrictive enough [;-)] to force sizeof(example1) to be 3?

No. The compiler is permitted to put in padding between members and
at the end "as necessary to achieve the appropriate alignment were the
structure or union to be an element of an array" (section 3.5.2.1). 
To do what you want, the standard would have to constrain implementors
to do this and no more, but it seems that the constraint is only that
it be sufficient, at least as I read it.

>Is anyone aware of existing compilers for which this wouldn't be true?

Sun's C compiler on a Sun 3 (with Sun OS 4.0) aligns structs on two
byte boundaries. This is NOT an ANSI-compatible compiler, but I don't
think that the ANSI standard will force them to change their ways. My
guess is that the implementation was done this way simply because it
was easier. Two-byte alignment is sufficient for any data type on the
Sun 3, and rarely wasts any storage since only char datatypes can get
by with only 1-byte alignment (your example is an exception).
Interestingly, with the Sun 4, your struct in fact has a size of 3,
even though the Sun 4 generally has more strict alignment rules than
the Sun 3.

Another compiler that uses a constant 2-byte alignment rule is the
Data General MV-series C compiler. Since the MV is a word-addressed
machine, this is the most reasonable implementation and permits struct
pointers to be word pointers. (Word and Character pointers have
different formats on the MV). Permitting an odd-sized struct would
require using character pointers instead of word pointers to structs,
which is less efficient in most instances.

>Is there a portability problem with the following structure where the
>array is intended to support a variable length array?
>
>	struct example2 { char a, b, c[1]; };

I think this approach is reasonably portable if you use offsetof
instead of relying on the sizeof() operator. E.g. to allocate the
structure allowing the array "c" to be "n" bytes long:

   #include <stddef.h>
   #include <stdlib.h>
   ...
   struct example2 { char a, b, c[1]; };
   struct example2 *ptr = malloc(offsetof(struct example2, c) + n);
-- 
Jim Patterson                              Cognos Incorporated
UUCP:decvax!utzoo!dciem!nrcaer!cognos!jimp P.O. BOX 9707    
PHONE:(613)738-1440                        3755 Riverside Drive
                                           Ottawa, Ont  K1G 3Z4



More information about the Comp.std.c mailing list