Date: Tue, 10 Mar 1998 15:09:19 -0500 From: John.Shue@symmetron.com (John A. Shue) To: "'grobin@accessv.com'" <grobin@accessv.com>, "questions@FreeBSD.ORG" <questions@FreeBSD.ORG> Subject: RE: sizeof(struct whatever) doesn't add up Message-ID: <01BD4C36.853CA110.John.Shue@symmetron.com>
next in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?01BD4C36.853CA110.John.Shue>