Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.lang.c.moderated > #466
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c.moderated |
| Subject | Re: Why sizeof(main) = 1? |
| Date | 2013-09-06 23:25 -0500 |
| Organization | None to speak of |
| Message-ID | <clcm-20130906-0003@plethora.net> (permalink) |
| References | <clcm-20130104-0002@plethora.net> <clcm-20130902-0002@plethora.net> |
<kzelechowski@e3tech.local> writes:
> (sizeof main == 1) because (main) is treated as a logical value. Compare:
> { if (main) yadda; }
No, that's not the reason.
Consider that an `int` can also be treated as a logical value;
`if (1)` is valid, but `sizeof 1` is the same as `sizeof (int)`,
which is rarely 1.
The expression `main`, or any function name, is implicitly converted,
in most contexts, to a pointer whose type depends on how you declared
and/or defined the function.
Any pointer value can be used as a condition; it's implicitly
checked for inequality to 0, i.e., to the null pointer.
if (main) yadda;
is well defined but not useful, since `main`, after conversion,
can never be a null pointer.
But when it's the argument of `&` or `sizeof`, this implicit
conversion doesn't happen. `&main` gives you the address of the
`main` function, just like `main` by itself does. `sizeof main`
*would* be the size of a function, but there is no such concept in C,
so it's a constraint violation, requiring a diagnostic.
> Użytkownik "Myth__Buster" napisał w wiadomości grup
> dyskusyjnych:clcm-20130104-0002@plethora.net...
>
> 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.
I think you mean pre C89 or pre-C90. C89/C90 introduced the `void`
keyword and the `void*` pointer type; C99 didn't make any real
changes in that area. But this has nothing to do with backward
compatibility; the same standard that introduced the `void` keyword
and type also prohibited applying `sizeof` to it.
(And `sizeof (char) == 1` by definition.)
`sizeof main == 1` because of a gcc extension. Standard C does
not permit pointer arithmetic on void* or on function pointers.
Gcc permits both, and specifies that, for example, adding 1 to a
void* or function pointer advances it by one byte (like adding 1 to
a char*). The authors of gcc chose to implement this by pretending
that the size of the type `void`, and the size of any function type,
is 1 (neither has a defined size in standard C).
I can see that pointer arithmetic on `void*` might be convenient in
some cases. The usefulness of arithmetic on function pointers is
less obvious, but I suppose it could have its uses. It's a pity
that implementing such arithmetic had the size effect of making
both `sizeof (void)` and `sizeof main` equal to 1; both results
are nonsensical.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
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 | Next — Previous in thread | Next in thread | Find similar
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