ftp.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2012/07/28/15:57:21

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 <eliz AT gnu DOT org>
Subject: Re: Upgrading from a bad C compiler
In-reply-to: <jv1ect$iv$1@speranza.aioe.org>
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> <jucp01$p35$1 AT speranza DOT aioe DOT org> <b1f8c389-45f4-43e4-9056-62fc50fc9462 AT googlegroups DOT com> <juh7c1$7hk$1 AT speranza DOT aioe DOT org> <3331145d-900b-4bef-8ad0-f533f0b4a17b AT googlegroups DOT com> <juuseq$j7b$1 AT speranza DOT aioe DOT org> <a7htthFse5U1 AT mid DOT dfncis DOT de> <jv1ect$iv$1 AT speranza DOT aioe DOT org>
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

> From: "Rod Pemberton" <do_not_have AT notemailnot DOT cmm>
> 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 <stdio.h>

int
main (void)
{
  long double foo;
  size_t lds = sizeof foo;

  printf ("sizeof long double = %d\n", lds);

  return 0;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019