X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f From: =?ISO-8859-15?Q?Hans-Bernhard_Br=F6ker?= Newsgroups: comp.os.msdos.djgpp Subject: Re: Upgrading from a bad C compiler Date: Sat, 28 Jul 2012 12:41:01 +0200 Lines: 39 Message-ID: References: <17d4b525-2c31-4c20-b3c5-a7118343e9a5 AT googlegroups DOT com> <3331145d-900b-4bef-8ad0-f533f0b4a17b AT googlegroups DOT com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.dfncis.de /PRQJm4CWi2qUBQ51Civxw+LFUDAcehnahEsZ5UjqqL+FFLAoL4mtnlsxIZYk0GhBc8MeqB+lV Cancel-Lock: sha1:uEvXYL4P0IST6/CaPkdbghK2qE4= User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20120604 Thunderbird/13.0 In-Reply-To: Bytes: 2790 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk On 27.07.2012 22:09, Rod Pemberton wrote: > I didn't know that, but I avoid sizeof(). Frankly, that's a pretty silly position. > 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". Whether or not that argument can possibly make sense depends on what you mean by "C" here: the C source code, the C translation tools, or some combination of the two? Yes, C source code must not know such things all by itself. But the C tools obviously must know (or they couldn't support structs at all). But sometimes the C source needs a way to inquire the info from the C tools, e.g. to dynamically allocate (arrays of) structs. The sizeof() operator is this way. > Either way, I see sizeof() leading to problems. You got that backwards. sizeof() is what solves those problems. The actual problem is with people who believe they know better than the C compiler what the actual size of a particular data structure must be, and therefore come here (and elsewhere) to complain that their compiler's sizeof() were buggy. > 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. Those people have understood what sizeof() is for. > 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. Such people have to sum the sizes of individual elements themselves.