From owner-freebsd-questions Tue Mar 10 12:08:16 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id MAA12586 for freebsd-questions-outgoing; Tue, 10 Mar 1998 12:08:16 -0800 (PST) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from mail.symmetron.com (ftp.symmetron.com [206.239.186.2]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id MAA12484 for ; Tue, 10 Mar 1998 12:07:58 -0800 (PST) (envelope-from John.Shue@symmetron.com) Received: from mail ([127.0.0.1]) by mail.symmetron.com (Netscape Mail Server v2.02) with SMTP id AAA217; Tue, 10 Mar 1998 15:09:37 -0500 Received: by localhost with Microsoft MAPI; Tue, 10 Mar 1998 15:09:28 -0500 Message-ID: <01BD4C36.853CA110.John.Shue@symmetron.com> From: John.Shue@symmetron.com (John A. Shue) Reply-To: "John.Shue@symmetron.com" To: "'grobin@accessv.com'" , "questions@FreeBSD.ORG" Subject: RE: sizeof(struct whatever) doesn't add up Date: Tue, 10 Mar 1998 15:09:19 -0500 Organization: Symmetron, Inc. X-Mailer: Microsoft Internet E-mail/MAPI - 8.0.0.4211 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Tuesday, March 10, 1998 2:17 PM, Geoffrey Robinson [SMTP:grobin@accessv.com] wrote: > I've started working on a CGI in C that uses large structures (over 1kb) > to hold statistics. I've noticed in previous projects that when I > attempt to get the size of a structured variable the value sizeof() > returns is usually a few bytes more than the sum I get if I manually add > up the sizes of the structure members. Since it worked anyway I passed > the extra bytes off as something that C needed to keep track of the > structure. But now that I'm using much bigger structures the extra bytes > account for as much as 50% of one of my structures. There is no mention > of anything like this in my C text. What are those extra bytes and, if > possible, how do I get rid of them? sounds like your problem is based on the fact that structures are aligned on 4 or 8-byte boundaries. I'm not sure which is the default for gcc (most likely 8). this means that when a structure doesn't have a multiple of 8 bytes, it is padded. If you have a large number of these padded structures, your sizeof() will be a lot larger than the hand calculated sizeof() the individual elements. -john --------------------- John A. Shue Symmetron, Inc. 4000 Legato Rd, Suite 600 Fairfax, VA 22033-4003 (703) 591-5559 To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message