Connecting Tech Pros Worldwide Forums | Help | Site Map

sizeof argument

Ben C
Guest
 
Posts: n/a
#1: Mar 25 '06
Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

int main(void)
{
printf("%d\n", sizeof(int()));
return 0;
}

I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
curious.

Thanks in advance for any explanations.

Guillaume
Guest
 
Posts: n/a
#2: Mar 25 '06

re: sizeof argument


Ben C wrote:[color=blue]
> Why does this compile with no warnings, what, if anything, does it mean,
> and why does the program print out 1?
>
> #include <stdio.h>
>
> int main(void)
> {
> printf("%d\n", sizeof(int()));
> return 0;
> }
>
> I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
> curious.[/color]

Good one. ;-)

'int ()' is actually a function type. (A function taking no argument
and returning an int.)

sizeof on a function type is undefined behavior. Whether it returns
1 or 10000, doesn't matter. It makes no sense.

With a appropriate compiler options, you should get a warning:
for instance, with GCC:

gcc -Wall -pedantic

Test1.c: In function `main':
Test1.c:5: warning: invalid application of `sizeof' to a function type

santosh
Guest
 
Posts: n/a
#3: Mar 25 '06

re: sizeof argument


Ben C wrote:[color=blue]
> Why does this compile with no warnings, what, if anything, does it mean,
> and why does the program print out 1?
>
> #include <stdio.h>
>
> int main(void)
> {
> printf("%d\n", sizeof(int()));
> return 0;
> }
>
> I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
> curious.[/color]

Maybe your warning level is not high enough? For me gcc with -Wall
-ansi and -pedantic options reports:
2.c: In function `main':
2.c:5: warning: invalid application of `sizeof' to a function type

But it compiles and prints 1 as output when run.

Martin Ambuhl
Guest
 
Posts: n/a
#4: Mar 25 '06

re: sizeof argument


Ben C wrote:[color=blue]
> Why does this compile with no warnings,[/color]

Because you have not got your diagnositics turned on.
gcc, even with the source language specified as gnu99,
warns

In function 'main':
5: warning: invalid application of 'sizeof' to a function type
5: warning: format '%d' expects type 'int', but argument 2 has type
'long unsigned int'

[color=blue]
> what, if anything, does it mean,[/color]

nothing, except as a non-portable extension provided by your implementation
[color=blue]
> and why does the program print out 1?[/color]

because that's what you implementation decided to interpret this
non-portable construct as meaning.
[color=blue]
>
> #include <stdio.h>
>
> int main(void)
> {
> printf("%d\n", sizeof(int()));
> return 0;
> }
>
> I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
> curious.
>
> Thanks in advance for any explanations.[/color]

Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human beings
do not use it.

Ben C
Guest
 
Posts: n/a
#5: Mar 25 '06

re: sizeof argument


On 2006-03-25, Guillaume <> wrote:[color=blue]
> Ben C wrote:[color=green]
>> Why does this compile with no warnings, what, if anything, does it mean,
>> and why does the program print out 1?
>>
>> #include <stdio.h>
>>
>> int main(void)
>> {
>> printf("%d\n", sizeof(int()));
>> return 0;
>> }
>>[/color]
> [...]
> 'int ()' is actually a function type. (A function taking no argument
> and returning an int.)
>
> sizeof on a function type is undefined behavior. Whether it returns
> 1 or 10000, doesn't matter. It makes no sense.[/color]

Of course, a function type! I was using -Wall but not -pedantic. Thanks.

The original example (which is on comp.programming) was this:

typedef void (*voidf)();
... sizeof (voidf()) ...

Well, not sizeof (voidf()) actually, but va_arg(ap, voidf()).

Here voidf() is also a function type-- a function that returns a pointer
to a function that returns void and takes no arguments that takes no
arguments. Surely not what was intended.
Guillaume
Guest
 
Posts: n/a
#6: Mar 25 '06

re: sizeof argument


Ben C wrote:[color=blue]
> Of course, a function type! I was using -Wall but not -pedantic. Thanks.[/color]

Yet another one of these "GCCisms". I don't think this should be allowed
at all. A warning doesn't seem enough to me here: and you only get one
if you go "-pedantic" with GCC. Weird.

Some other C compilers report this construct as an error, and I think
they should as well.
pemo
Guest
 
Posts: n/a
#7: Mar 26 '06

re: sizeof argument


Martin Ambuhl wrote:[color=blue]
> Ben C wrote:[color=green]
>> Why does this compile with no warnings,[/color]
>
> Because you have not got your diagnositics turned on.
> gcc, even with the source language specified as gnu99,
> warns
>
> In function 'main':
> 5: warning: invalid application of 'sizeof' to a function type
> 5: warning: format '%d' expects type 'int', but argument 2 has type
> 'long unsigned int'
>
>[color=green]
>> what, if anything, does it mean,[/color]
>
> nothing, except as a non-portable extension provided by your
> implementation[color=green]
>> and why does the program print out 1?[/color]
>
> because that's what you implementation decided to interpret this
> non-portable construct as meaning.
>[color=green]
>>
>> #include <stdio.h>
>>
>> int main(void)
>> {
>> printf("%d\n", sizeof(int()));
>> return 0;
>> }
>>
>> I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
>> curious.
>>
>> Thanks in advance for any explanations.[/color]
>
> Bill collectors write "thanks in advance." It is language used to
> create dominance in the expectation of compliance. Polite human
> beings do not use it.[/color]

Who's 'Bill Collectors' - any relation to 'Bill Posters?

--
==============
*Not a pedant*
==============


Rod Pemberton
Guest
 
Posts: n/a
#8: Mar 26 '06

re: sizeof argument



"Martin Ambuhl" <mambuhl@earthlink.net> wrote in message
news:lehVf.17705$S25.15805@newsread1.news.atl.eart hlink.net...[color=blue][color=green]
> >
> > Thanks in advance for any explanations.[/color]
>
> Bill collectors write "thanks in advance." It is language used to
> create dominance in the expectation of compliance. Polite human beings
> do not use it.[/color]

Wow. And, Plauger told me, "My, you do have a twisty way of thinking, don't
you?"

I may not be the correct person from your perspective to defend BenC, but, I
haven't seen a harsh word yet. I'd say it's a polite statement and nothing
more.



jaysome
Guest
 
Posts: n/a
#9: Mar 26 '06

re: sizeof argument


Martin Ambuhl wrote:

[snip]
[color=blue]
> Bill collectors write "thanks in advance." It is language used to
> create dominance in the expectation of compliance. Polite human beings
> do not use it.[/color]

Really?

I guess that people like myself and probably the OP who have never
talked to or heard from a bill collector should be expected to know
better. Where did we go wrong?

--
jay
Default User
Guest
 
Posts: n/a
#10: Mar 26 '06

re: sizeof argument


Martin Ambuhl wrote:

[color=blue]
> Bill collectors write "thanks in advance." It is language used to
> create dominance in the expectation of compliance. Polite human
> beings do not use it.[/color]

I'm going to have to call BS that. I don't believe it's in any way
impolite.



Brian
--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Ben C
Guest
 
Posts: n/a
#11: Mar 26 '06

re: sizeof argument


On 2006-03-26, Rod Pemberton <do_not_have@sorry.bitbuck.cmm> wrote:[color=blue]
>
> "Martin Ambuhl" <mambuhl@earthlink.net> wrote in message
> news:lehVf.17705$S25.15805@newsread1.news.atl.eart hlink.net...[color=green][color=darkred]
>> >
>> > Thanks in advance for any explanations.[/color]
>>
>> Bill collectors write "thanks in advance." It is language used to
>> create dominance in the expectation of compliance. Polite human beings
>> do not use it.[/color]
>
> Wow. And, Plauger told me, "My, you do have a twisty way of thinking, don't
> you?"
>
> I may not be the correct person from your perspective to defend BenC, but, I
> haven't seen a harsh word yet. I'd say it's a polite statement and nothing
> more.[/color]

Thank you! Certainly no implication of dominance or compliance was
intended, I am sorry if anyone took it that way.
Keith Thompson
Guest
 
Posts: n/a
#12: Mar 26 '06

re: sizeof argument


Ben C <spamspam@spam.eggs> writes:[color=blue]
> On 2006-03-26, Rod Pemberton <do_not_have@sorry.bitbuck.cmm> wrote:[color=green]
>>
>> "Martin Ambuhl" <mambuhl@earthlink.net> wrote in message
>> news:lehVf.17705$S25.15805@newsread1.news.atl.eart hlink.net...[color=darkred]
>>> >
>>> > Thanks in advance for any explanations.
>>>
>>> Bill collectors write "thanks in advance." It is language used to
>>> create dominance in the expectation of compliance. Polite human beings
>>> do not use it.[/color]
>>
>> Wow. And, Plauger told me, "My, you do have a twisty way of thinking, don't
>> you?"
>>
>> I may not be the correct person from your perspective to defend BenC, but, I
>> haven't seen a harsh word yet. I'd say it's a polite statement and nothing
>> more.[/color]
>
> Thank you! Certainly no implication of dominance or compliance was
> intended, I am sorry if anyone took it that way.[/color]

Some people are offended by the phrase "thanks in advance", thinking
it implies some sort of obligation. Others (myself included) don't
think it's at all a big deal. (I occasionally use "AtDhVaAnNkCsE"
myself.) I suggest that those who use the term should probably avoid
it, and those who are offended by it should consider that it's
probably not meant to be offensive.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Closed Thread