From owner-freebsd-questions Sun Mar 14 17: 6:25 1999 Delivered-To: freebsd-questions@freebsd.org Received: from alpha.comkey.com.au (alpha.comkey.com.au [203.9.152.215]) by hub.freebsd.org (Postfix) with SMTP id A18D91531E for ; Sun, 14 Mar 1999 17:06:18 -0800 (PST) (envelope-from gjb@comkey.com.au) Received: (qmail 16893 invoked by uid 1001); 15 Mar 1999 01:03:09 -0000 Message-ID: <19990315010309.16892.qmail@alpha.comkey.com.au> X-Posted-By: GBA-Post 1.04 06-Feb-1999 X-PGP-Fingerprint: 5A91 6942 8CEA 9DAB B95B C249 1CE1 493B 2B5A CE30 Date: Mon, 15 Mar 1999 11:03:09 +1000 From: Greg Black To: Mark Turpin Cc: questions@FreeBSD.ORG Subject: Re: Programming Question References: In-reply-to: of Sun, 14 Mar 1999 16:50:32 EST Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > 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 To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message