Groups | Search | Server Info | Login | Register


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

Re: Why sizeof(main) = 1?

From Barry Schwarz <schwarzb@dqel.com>
Newsgroups comp.lang.c.moderated
Subject Re: Why sizeof(main) = 1?
Date 2013-02-26 10:51 -0600
Organization SunSITE.dk - Supporting Open source
Message-ID <clcm-20130226-0003@plethora.net> (permalink)
References <clcm-20130104-0002@plethora.net>

Show all headers | View raw


On Fri, 4 Jan 2013 18:14:52 -0600 (CST), Myth__Buster
<raghavanil4m@gmail.com> wrote:

>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?

sizeof(char) is always 1, not just usually.  Why do you think
sizeof(char) being 1 has any effect on sizeof(void*) or sizeof(char*)?

>Also, I tried the invariably buggy code to see if at all sizeof(main)
>= 1 can be realized.
>
>/* Keep the warnings aside for a minute, please! */
>#include <stdio.h>
>
>int main(void)
>{
>        printf("sizeof(char) : %zd\n", sizeof(char));
>        printf("sizeof(main) : %zd\n", sizeof(main));

This second argument contains a constraint violation and therefore
requires a diagnostic.  (The sizeof operator cannot be applied to an
expression of type function.)  After that, there is no correct result
so 1 is as good as any other value.

>        char p = main; // Truncates into only one byte of the bytes

This assignment also contains a constraint violation and requires a
diagnostic.   (There is no implicit conversion from function pointer
to char.)

>required to hold
>                              // main's address - known bug. But just
>to see sizeof(main) = 1 if at all makes any sense.
>
>        ((int (*)(void))p)(); // Known to be buggy and wrong.

This is a syntactically valid statement but since the value in p does
not actually point to a function, once the statement executes you have
undefined behavior.

>        return 0;
>}
>
>Any explanation is cheerful. :)

Step outside the language and get strange results.  Why are you
surprised?

-- 
Remove del for email
-- 
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 | Next in thread | Find similar


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