Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c.moderated > #428
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c.moderated |
| Subject | Re: Why sizeof(main) = 1? |
| Date | 2013-03-11 18:25 -0500 |
| Organization | None to speak of |
| Message-ID | <clcm-20130311-0001@plethora.net> (permalink) |
| References | <clcm-20130104-0002@plethora.net> <clcm-20130226-0003@plethora.net> |
Barry Schwarz <schwarzb@dqel.com> writes:
> On Fri, 4 Jan 2013 18:14:52 -0600 (CST), Myth__Buster
> <raghavanil4m@gmail.com> wrote:
>>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.
[...]
I can't see the parent article, which was probably posted some time ago,
with responses appearnig only now because comp.lang.c.moderated tends to
have a rather long latency. Quite possibly the answer has already been
posted, but I don't see it here, so ...
gcc supports arithmetic on void* and on pointer-to-function types as a
language extension. In standard C, any attempt to perform pointer
arithmetic on a function pointer or on a pointer to an incomplete type
(such as void*) is a constraint violation, requiring a diagnostic.
gcc is *not* a conforming compiler by default. If you provide the right
command-line options (something like "-std=cXX -pedantic", where XX can
be 89, 99, or 11), then it attempt to be conforming and will warn about
such things.
But wait, you're not performing pointer arithmetic, so why does this
matter? C defines pointer arithmetic in terms of the size of the
poitner's referenced type. The expression `main` is of
pointer-to-function type in this context. The gcc developers decided to
make `sizeof main` 1; the behavior of pointer arithmitec on a
pointer-to-function type follows from that. So `main + 1` is a function
pointer that points one byte *after* the address of the main function.
But only in GNU C; in standard C `main + 1` is nonsense.
IMHO this gcc extension provides a very small amount of convenience for
non-portable code at the expense of a great deal of confusion.
--
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 | Unroll 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