Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c.moderated > #469

Re: Why sizeof(main) = 1?

From Ken Brody <kenbrody@spamcop.net>
Newsgroups comp.lang.c.moderated
Subject Re: Why sizeof(main) = 1?
Date 2013-09-06 23:25 -0500
Organization A noiseless patient Spider
Message-ID <clcm-20130906-0006@plethora.net> (permalink)
References <clcm-20130104-0002@plethora.net> <clcm-20130902-0002@plethora.net>

Show all headers | View raw


On 9/2/2013 5:07 AM, kzelechowski@e3tech.local wrote:
> (sizeof main == 1) because (main) is treated as a logical value.  Compare: {
> if (main) yadda; }

No, it is 1 because gcc has decided that, as an extension to the C standard, 
it will allow sizeof to be applied to functions, and will be the value 1.

6.5.3.4p1 states:

     "The sizeof operator shall not be applied to an expression that has
     function type or an incomplete type, to the parenthesized name of
     such a type, or to an expression that designates a bit-field member."

I believe that the name of a function is an "expression that has function 
type".  (Though I'm sure I will be quickly corrected if I am mistaken.)

If I try compiling the following code:

====
extern void foo(void);
int x = (int)sizeof foo;
====

I get the error:

     'void (__cdecl *)(void)': illegal sizeof operand

>
> Użytkownik "Myth__Buster"  napisał w wiadomości grup
> dyskusyjnych:clcm-20130104-0002@plethora.net...
>
> Hi,
>
> On a Linux system with gcc, I am just wondering why sizeof(main) can
> be 1 or sizeof when applied on any function name can yield 1 ever? Or
> is it only gcc's perspective to say sizeof of an implicit function
> pointer to be 1 since it gives sizeof(void) to be 1 based on the
> backward compatibility with the pre C99 notion that void* had its
> predecessor char* and usually sizeof(char) being 1?
>
> Also, I tried the invariably buggy code to see if at all sizeof(main)
> = 1 can be realized.
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.

Back to comp.lang.c.moderated | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Why sizeof(main) = 1? Myth__Buster <raghavanil4m@gmail.com> - 2013-01-04 18:14 -0600
  Re: Why sizeof(main) = 1? Barry Schwarz <schwarzb@dqel.com> - 2013-02-26 10:51 -0600
    Re: Why sizeof(main) = 1? Keith Thompson <kst-u@mib.org> - 2013-03-11 18:25 -0500
  Re: Why sizeof(main) = 1? Jasen Betts <jasen@xnet.co.nz> - 2013-02-26 10:51 -0600
  Re: Why sizeof(main) = 1? gordonb.k8xjg@burditt.org (Gordon Burditt) - 2013-02-26 10:51 -0600
  Re: Why sizeof(main) = 1? <kzelechowski@e3tech.local> - 2013-09-02 04:07 -0500
    Re: Why sizeof(main) = 1? James Kuyper <jameskuyper@verizon.net> - 2013-09-06 23:25 -0500
      Re: Why sizeof(main) = 1? Keith Thompson <kst-u@mib.org> - 2013-09-11 17:26 -0500
        Re: Why sizeof(main) = 1? James Kuyper <jameskuyper@verizon.net> - 2013-09-12 11:29 -0500
    Re: Why sizeof(main) = 1? Keith Thompson <kst-u@mib.org> - 2013-09-06 23:25 -0500
      Re: Why sizeof(main) = 1? Robert Wessel <robertwessel2@yahoo.com> - 2013-09-11 17:27 -0500
        Re: Why sizeof(main) = 1? James Kuyper <jameskuyper@verizon.net> - 2013-09-12 11:29 -0500
        Re: Why sizeof(main) = 1? Keith Thompson <kst-u@mib.org> - 2013-09-12 11:30 -0500
    Re: Why sizeof(main) = 1? Ken Brody <kenbrody@spamcop.net> - 2013-09-06 23:25 -0500

csiph-web