Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof() O/P

Reply
Thread Tools

sizeof() O/P

 
 
Eric Sosman
Guest
Posts: n/a
 
      12-24-2005
Giannis Papadopoulos wrote:
> Eric Sosman wrote:
>> Here's a cleaned-up version:
>>
>> #include <stdio.h> /* for printf() prototype */
>> int main(void) {
>> printf ("%d\n", /* note newline */
>> (int)sizeof printf()); /* note type coercion */
>> return 0; /* required in C90, optional in C99 */
>> }

>
> Well, this does not compile on gcc given -ansi -pedantic.
> Perhaps printf("") instead of printf() would be better?


Interesting. The constraint of 6.5.2.2/2 requires that
the number and type of the provided function arguments agree
with those of the function prototype, so it seems a diagnostic
is required. On the other hand, the erroneous call produces
no executable code and cannot possibly cause harm (and the
compiler knows this). The question comes down to whether
6.5.2.2/2 applies to function calls that appear in the code
(even if only in a formal sense) or should be taken as applying
to function calls that are executed at run-time.

Full employment for language lawyers, I guess. In any
case, good catch. By my count, the O.P.'s code contains five
known (now) errors in just four lines, only two non-trivial.
Maybe the Guinness people should be alerted after all.

--
 
Reply With Quote
 
 
 
 
Randy Howard
Guest
Posts: n/a
 
      12-24-2005
Richard Heathfield wrote
(in article
<dojjvj$qeo$(E-Mail Removed)-infra.bt.com>):

> Chris Hills said:
>
>> In article <(E-Mail Removed) .com>,
>> raghu <(E-Mail Removed)> writes
>>> i'm surprised at the output of the following code. compiled in turbo C
>>>
>>> void main()
>>> {
>>> printf("%d",sizeof(printf());
>>> }
>>> the output was : 2
>>> how come the output is 2? actually what is the property of sizeof()
>>> and printf()?are there any return types for the two functions? hoping
>>> for the positive responses.. thanks a lot in advance.

>>
>> Ignoring that it should have been
>>
>> #include <stdio.h>
>> int main(void)
>> {
>> printf("%d",sizeof(printf());

>
> No, %d doesn't match size_t.


MISRA loophole?


--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      12-24-2005

"raghu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> i'm surprised at the output of the following code. compiled in turbo C
>
> void main()
> {
> printf("%d",sizeof(printf());
> }
> the output was : 2
> how come the output is 2? actually what is the property of sizeof()
> and printf()?are there any return types for the two functions? hoping
> for the positive responses.. thanks a lot in advance.


This reminds me of an 'incident' [a very long time ago now!] when a fellow
lecturer of mine [who's now a *very* highly respected prof. of electrical
engineering] said to me ... "you know what, there's a major bug in the
compiler! A student of mine had the following [see below] and the damn
compiler said it was ok!!!!

Boy, it took me ages to find out what was *wrong*.

I should report it as a major bug!!!"

===

Here's the line of code

void someFunc(void)
{
}

int main(void)
{
...
...
/* error - damn s**t compiler [his implicit annotation] */
someFunc;
...
...
return you_know_what;
}


 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      12-24-2005
On 2005-12-24, pemo <(E-Mail Removed)> wrote:
>
> "raghu" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> i'm surprised at the output of the following code. compiled in turbo C
>>
>> void main()
>> {
>> printf("%d",sizeof(printf());
>> }
>> the output was : 2
>> how come the output is 2? actually what is the property of sizeof()
>> and printf()?are there any return types for the two functions? hoping
>> for the positive responses.. thanks a lot in advance.

>
> This reminds me of an 'incident' [a very long time ago now!] when a fellow
> lecturer of mine [who's now a *very* highly respected prof. of electrical
> engineering] said to me ... "you know what, there's a major bug in the
> compiler! A student of mine had the following [see below] and the damn
> compiler said it was ok!!!!
>
> Boy, it took me ages to find out what was *wrong*.
>
> I should report it as a major bug!!!"
>
>===
>
> Here's the line of code
>
> void someFunc(void)
> {
>}
>
> int main(void)
> {
> ...
> ...
> /* error - damn s**t compiler [his implicit annotation] */
> someFunc;
> ...
> ...
> return you_know_what;
>}


What's wrong with that? It evaluates the address of the function and
then throws it away. [of course, if the compiler makes it _call_ the
function, that's a problem]
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-24-2005
Eric Sosman <(E-Mail Removed)> writes:
> Giannis Papadopoulos wrote:
>> Eric Sosman wrote:
>>> Here's a cleaned-up version:
>>>
>>> #include <stdio.h> /* for printf() prototype */
>>> int main(void) {
>>> printf ("%d\n", /* note newline */
>>> (int)sizeof printf()); /* note type coercion */
>>> return 0; /* required in C90, optional in C99 */
>>> }

>> Well, this does not compile on gcc given -ansi -pedantic.
>> Perhaps printf("") instead of printf() would be better?

>
> Interesting. The constraint of 6.5.2.2/2 requires that
> the number and type of the provided function arguments agree
> with those of the function prototype, so it seems a diagnostic
> is required. On the other hand, the erroneous call produces
> no executable code and cannot possibly cause harm (and the
> compiler knows this). The question comes down to whether
> 6.5.2.2/2 applies to function calls that appear in the code
> (even if only in a formal sense) or should be taken as applying
> to function calls that are executed at run-time.


It's a constraint, requiring a compile-time diagnostic. Of course it
applies at compilation time.

All three printf calls in the following violate the same constraint:

#include <stdio.h>
#include <time.h>
int main(void)
{
sizeof printf();
if (0) {
printf();
}
if (time(NULL) < 1135455446) {
printf();
}
}

In all three cases, the compiler can potentially determine that the
call is never executed (for the last, it would have to take advantage
of its knowledge about the representation of time_t and assume that
the system clock is correct). This doesn't relieve the compiler of
its obligation to generate a diagnostic, any more than it could omit a
diagnostic for a conditional syntax error:

if (0) {
printf("hello, world\n) /* missing semicolon */
}

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-24-2005
Emmanuel Delahaye <(E-Mail Removed)> writes:
> raghu a écrit :
>> i'm surprised at the output of the following code. compiled in turbo C
>> void main()
>> {
>> printf("%d",sizeof(printf());
>> }

>
> This code invokes an undefined behaviour
>
> - The type returned by main() is int.
> - An explicit valid value must be returned.
> "%d" expects an int and the sizeof operator returns a size_t.


But printf is never actually called, and without a visible prototype
the compiler (at least for C90) assumes that printf() is a function
taking unknown arguments and returning int.

In fact, the following returns sizeof(int) to the calling environment:

int main(void)
{
return sizeof unknown_function();
}

On the system where I just tried it, since unknown_function() isn't
actually called, the implementation doesn't attempt to link it into
the executable. I'm not sure whether this is allowed (and it's too
bizarre for me to care very much).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      12-24-2005
pemo wrote:
> "raghu" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
>>i'm surprised at the output of the following code. compiled in turbo C
>>
>>void main()
>>{
>>printf("%d",sizeof(printf());
>>}
>>the output was : 2
>>how come the output is 2? actually what is the property of sizeof()
>>and printf()?are there any return types for the two functions? hoping
>>for the positive responses.. thanks a lot in advance.

>
>
> This reminds me of an 'incident' [a very long time ago now!] when a fellow
> lecturer of mine [who's now a *very* highly respected prof. of electrical
> engineering] said to me ... "you know what, there's a major bug in the
> compiler! A student of mine had the following [see below] and the damn
> compiler said it was ok!!!!
>
> Boy, it took me ages to find out what was *wrong*.
>
> I should report it as a major bug!!!"
>
> ===
>
> Here's the line of code
>
> void someFunc(void)
> {
> }
>
> int main(void)
> {
> ...
> ...
> /* error - damn s**t compiler [his implicit annotation] */
> someFunc;


Almost all compilers I've seen have an option for issuing a warning when a
statement has no effect, typically enabled with a host of other useful
warnings you wouldn't want to do without. Aside from a better grasp of the
language, a better understanding of the compiler wouldn't hurt either.

S.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-24-2005
On Sat, 24 Dec 2005 20:26:26 GMT, in comp.lang.c , Keith Thompson
<(E-Mail Removed)> wrote:

>On the system where I just tried it, since unknown_function() isn't
>actually called, the implementation doesn't attempt to link it into
>the executable. I'm not sure whether this is allowed


I suspect the 'as if' rule would apply. Since the code behaves as if
the function didn't do anything other than return an unspecified int,
the compiler is allowed to completely optimise it out and replace it
by an unspecified int. OTOH I'd expect some sort of complaint.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      12-25-2005

"Jordan Abel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2005-12-24, pemo <(E-Mail Removed)> wrote:
>>
>> "raghu" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>>> i'm surprised at the output of the following code. compiled in turbo C
>>>
>>> void main()
>>> {
>>> printf("%d",sizeof(printf());
>>> }
>>> the output was : 2
>>> how come the output is 2? actually what is the property of sizeof()
>>> and printf()?are there any return types for the two functions? hoping
>>> for the positive responses.. thanks a lot in advance.

>>
>> This reminds me of an 'incident' [a very long time ago now!] when a
>> fellow
>> lecturer of mine [who's now a *very* highly respected prof. of electrical
>> engineering] said to me ... "you know what, there's a major bug in the
>> compiler! A student of mine had the following [see below] and the damn
>> compiler said it was ok!!!!
>>
>> Boy, it took me ages to find out what was *wrong*.
>>
>> I should report it as a major bug!!!"
>>
>>===
>>
>> Here's the line of code
>>
>> void someFunc(void)
>> {
>>}
>>
>> int main(void)
>> {
>> ...
>> ...
>> /* error - damn s**t compiler [his implicit annotation] */
>> someFunc;
>> ...
>> ...
>> return you_know_what;
>>}

>
> What's wrong with that? It evaluates the address of the function and
> then throws it away. [of course, if the compiler makes it _call_ the
> function, that's a problem]


Um, yes, I know!


 
Reply With Quote
 
Jan Engelhardt
Guest
Posts: n/a
 
      12-25-2005
>i'm surprised at the output of the following code. compiled in turbo C
>
>void main()
>{
>printf("%d",sizeof(printf());
>}
>the output was : 2


Given that <stdio.h> was included, the return type of printf is int,
and an int in turbo C is a 16-bit entity.

>how come the output is 2? actually what is the property of sizeof()


Try again on a modern compiler and you should get 4.



Jan Engelhardt
--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments