[comp.lang.c] Size of structure containing char fields

bds@lzaz.ATT.COM (Bruce Szablak) (07/17/90)

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?

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

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]; };

Thanks in advance.

henry@zoo.toronto.edu (Henry Spencer) (07/18/90)

In article <1030@lzaz.ATT.COM> bds@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?
>Is anyone aware of existing compilers for which this wouldn't be true?

ANSI more or less leaves the question open.  Machines on which char
pointers are strange will almost certainly pad the structure to a word
boundary so they can use "normal" pointer format for `struct example1 *'.
Some compilers on more ordinary machines may do likewise.  Some won't.
-- 
NFS:  all the nice semantics of MSDOS, | Henry Spencer at U of Toronto Zoology
and its performance and security too.  |  henry@zoo.toronto.edu   utzoo!henry

jimp@cognos.UUCP (Jim Patterson) (07/24/90)

In article <1030@lzaz.ATT.COM> bds@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