From owner-freebsd-questions Sun Mar 14 17:32:15 1999 Delivered-To: freebsd-questions@freebsd.org Received: from smtp1.vnet.net (smtp1.vnet.net [166.82.1.31]) by hub.freebsd.org (Postfix) with ESMTP id 040D915CBD for ; Sun, 14 Mar 1999 17:32:12 -0800 (PST) (envelope-from rivers@dignus.com) Received: from dignus.com (ponds.vnet.net [166.82.177.48]) by smtp1.vnet.net (8.9.1a/8.9.1) with ESMTP id UAA28813; Sun, 14 Mar 1999 20:31:48 -0500 (EST) Received: from lakes.dignus.com (lakes.dignus.com [10.0.0.3]) by dignus.com (8.9.2/8.8.5) with ESMTP id UAA06340; Sun, 14 Mar 1999 20:31:46 -0500 (EST) Received: (from rivers@localhost) by lakes.dignus.com (8.9.2/8.6.9) id UAA10509; Sun, 14 Mar 1999 20:31:44 -0500 (EST) Date: Sun, 14 Mar 1999 20:31:44 -0500 (EST) From: Thomas David Rivers Message-Id: <199903150131.UAA10509@lakes.dignus.com> To: ian@bulinfo.net, mturpin@saturn.spel.com Subject: Re: Programming Question Cc: freebsd-questions@FreeBSD.ORG In-Reply-To: <36EC34C0.13D218B4@bulinfo.net> Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > Mark Turpin wrote: > > > > I feel really silly having to ask this. > > > > Why does my sizeof(mystruct) come out as 4 instead of 3 ( short + char ) ? > > > > struct { > > short shortvariable; > > char charvariable; > > } mystruct; > > > > sizeof(mystruct) == 4 > > Try finding the addresses of the structure members. As I know the compiler > aligns every variable at even address. Maybe that's the reason. Not exactly... the size of the structure depends on the maximum alignment requirements of all of its members. In this case; on most architectures, the alignment of the `shortvariable' is 2, the alignment of `charvariable' is 1. So, the alignment of the entire structure is 2. The size of the structure is then padded - so that if you had an array of these structures, each array element would be properly aligned so that the structure alignment is preserved. Thus, the size of the structure is padded to reach a two-byte alignment... adding one byte... making the size 4. That's why; when dealing with structures - the whole is typically greater than the sum of the parts... :-) - Dave Rivers - To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message