X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f X-Recipient: djgpp AT delorie DOT com Date: Sat, 28 Jul 2012 22:57:20 +0300 From: Eli Zaretskii Subject: Re: Upgrading from a bad C compiler In-reply-to: X-012-Sender: halo1 AT inter DOT net DOT il To: djgpp AT delorie DOT com Message-id: <83lii3hd5r.fsf@gnu.org> References: <17d4b525-2c31-4c20-b3c5-a7118343e9a5 AT googlegroups DOT com> <3331145d-900b-4bef-8ad0-f533f0b4a17b AT googlegroups DOT com> 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 > From: "Rod Pemberton" > Date: Sat, 28 Jul 2012 15:28:28 -0400 > > sizeof() is clearly adequate for returning the correct sizes of > basic types. It is adequate for any kind of data type. > But, my usage of it is minimal, only where absolutely needed. This can be said about every feature of every programming language -- they should be used only when necessary. > Why would a C programmer assume sizeof() for a union or struct returns > anything other than exactly the "sum of the sizes of the individual > elements"? I'd say that's the most likely expectation of someone using > sizeof() on a struct, union, or typedef. That's what it does for int's, > char's, long's, etc. Yes? Can C return 7 due to padding for sizeof() of an > unsigned long? (Horrors...) What about sizeof() of a char? What if it > didn't return one ... ? If someone takes a sizeof() of a struct that has > three unsigned long's, they don't expect some astronomical value because of > padding or alignment, e.g., perhaps returning 64 on 64-bit aligned > machine... Do they? No, of course not. That's something that is learned. Try 'sizeof(long double)' some day. On a x86 machine, a 'long double' type is a 80-bit floating point type used to store FP values in FP registers, so according to you its sizeof should be 10, right? Now try the sample program at the end of this message. What does it tell you? What is the morale of this? #include int main (void) { long double foo; size_t lds = sizeof foo; printf ("sizeof long double = %d\n", lds); return 0; }