Date: Mon, 15 Mar 1999 11:03:09 +1000 From: Greg Black <gjb@comkey.com.au> To: Mark Turpin <mturpin@saturn.spel.com> Cc: questions@FreeBSD.ORG Subject: Re: Programming Question Message-ID: <19990315010309.16892.qmail@alpha.comkey.com.au> In-Reply-To: <Pine.BSF.4.05.9903141646550.6443-100000@saturn.spel.com> of Sun, 14 Mar 1999 16:50:32 EST References: <Pine.BSF.4.05.9903141646550.6443-100000@saturn.spel.com>
next in thread | previous in thread | raw e-mail | index | archive | help
> Why does my sizeof(mystruct) come out as 4 instead of 3 ( short + char ) ? > > struct { > short shortvariable; > char charvariable; > } mystruct; Because that's the size the compiler allocated for it. It could legitimately be 8 (and other sizes, too). Read clause 6.5.2.1 "Semantics" of ISO/IEC 9899: 1990 (the ISO Standard for C) for more info. In particular, note the paragraph that says: Each non-bit-field member of a structure or union object is aligned in an implementation-defined manner appropriate to its type. Then see the later paragraph that says: There may also be unnamed padding at the end of a structure or union, as necessary to achieve the appropriate alignment were the structure or union to be an element of an array. Because it's implementation-defined, compiler authors are at liberty to offer normal default behaviour with padding for alignment needs (as you have seen here) and optional packed behaviour, provided they document it. Final note: this has nothing to do with FreeBSD. Try a news group like comp.lang.c if you have Usenet access. -- Greg Black <gjb@acm.org> To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990315010309.16892.qmail>