🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #1519

Re: Size of structure containing char fields

#1519
From: Doug Gwyn
Date: Mon, 23 Jul 1990 18:23
23 lines
1555 bytes
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; there may be padding at the end of the structure.

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

Yes.  Practically any word-oriented architecture would have padding.

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

There is no problem with the declaration; however, care must be exerted
in using it for variable-sized allocations.  Karl Heuer (I think it was)
a few months ago gave a logical analysis in support of the view that the
C standard does permit this sort of trick if coded properly.  That is,
an attempt to enforce a bounds check on the array would be nonconforming
in such contexts.  I agree with the analysis, and have in fact used this
trick in code that was intended to be highly portable, but I don't
recall X3J11 specifically blessing this interpretation.
Technical Details
Message-ID: <13385@smoke.BRL.MIL>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!aplcen!haven!ncifcrf!lhc!adm!smoke!gwyn
References: <1030@lzaz.ATT.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!aplcen!haven!ncifcrf!lhc!adm!smoke!gwyn
From: gwyn@smoke.BRL.MIL (Doug Gwyn)
Newsgroups: comp.std.c
Subject: Re: Size of structure containing char fields
Message-ID: <13385@smoke.BRL.MIL>
Date: 23 Jul 90 18:23:23 GMT
References: <1030@lzaz.ATT.COM>
Organization: U.S. Army Ballistic Research Laboratory, APG, MD.
Lines: 23