X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f From: "Rod Pemberton" Newsgroups: comp.os.msdos.djgpp Subject: Re: Upgrading from a bad C compiler Date: Fri, 27 Jul 2012 16:09:59 -0400 Organization: Aioe.org NNTP Server Lines: 44 Message-ID: References: <17d4b525-2c31-4c20-b3c5-a7118343e9a5 AT googlegroups DOT com> <3331145d-900b-4bef-8ad0-f533f0b4a17b AT googlegroups DOT com> NNTP-Posting-Host: CNsg4fVcCsvs3UaOgZtQCw.user.speranza.aioe.org X-Complaints-To: abuse AT aioe DOT org X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.2001 X-Notice: Filtered by postfilter v. 0.8.2 X-Newsreader: Microsoft Outlook Express 6.00.2800.2001 X-Priority: 3 X-MSMail-Priority: Normal Bytes: 2822 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com "RayeR" wrote in message news:3331145d-900b-4bef-8ad0-f533f0b4a17b AT googlegroups DOT com... > RP wrote ... ... > > I'd assume alignment would increase the physical size. I don't > > know if it increases sizeof()'s value though. I would think not... > > I would think that C should only return the total size for C elements > > and not padding or alignment. > > No, sizeof() is called on a structure which is aligned by default it > returns total size including padding bytes! Usually 8bit members are > extended to 16bits to be placed on even address. If you need a structure > that match exact size (e.g. reading > some header from file to struct in > memory) use __attribute__ ((packed)). Then sizeof will return number > that is exact sum of all members size. I didn't know that, but I avoid sizeof(). That could've been the original reason why... One could definately argue C shouldn't "know" anything about the padding, i.e., it can be argued that padding is outside of C's "sphere of knowledge". Either way, I see sizeof() leading to problems. There will be those who want to know the exact underlying size of the aligned and padded struct so they can allocate space for more aligned and padded structs correctly. There will also be those who only want to know only the size of the struct's C components so that when they copy the structs C elements to an array or other object they don't have extra space included, etc. Sizeof won't work correctly for both situations, at least not for ANSI C. One or the other will have to do work work to get the correct "sizeof()" value for their situation. It will work with help from one of many possible compiler extensions, like '#pragma pack()'... Rod Pemberton