* sizeof( function )
@ 2007-06-12 16:12 Mihai Donțu
2007-06-12 16:38 ` Brian Dessent
0 siblings, 1 reply; 11+ messages in thread
From: Mihai Donțu @ 2007-06-12 16:12 UTC (permalink / raw)
To: gcc-help
Hi,
Not that is of great importance, but does anyone know why
int main( void )
{
return printf( "%d\n", ( int )sizeof( main ) );
}
prints 1 ? :)
(sizeof( &main ) prints 4/8 as it should)
--
Mihai DonÈu
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: sizeof( function )
2007-06-12 16:12 sizeof( function ) Mihai Donțu
@ 2007-06-12 16:38 ` Brian Dessent
2007-06-12 16:56 ` Segher Boessenkool
` (3 more replies)
0 siblings, 4 replies; 11+ messages in thread
From: Brian Dessent @ 2007-06-12 16:38 UTC (permalink / raw)
To: Mihai Don?u; +Cc: gcc-help
Mihai Don?u wrote:
> Not that is of great importance, but does anyone know why
>
> int main( void )
> {
> return printf( "%d\n", ( int )sizeof( main ) );
> }
>
> prints 1 ? :)
>
> (sizeof( &main ) prints 4/8 as it should)
In this context 'main' is a function type, i.e. a type that describes a
function of a given return type and a given number and type of arguments
(C99 §6.2.5 para. 20). It is invalid to use sizeof() with a function
type expression (C99 §6.5.3.4 para. 1) and hence this code invokes
undefined behavior. gcc gives a warning if you compile with -pedantic,
but otherwise it apparently just returns 1; it would be equally within
its rights to return 42 or to generate code to format the users hard
drive -- that is the nature of undefined behavior.
Brian
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: sizeof( function )
2007-06-12 16:38 ` Brian Dessent
@ 2007-06-12 16:56 ` Segher Boessenkool
2007-06-12 17:05 ` Mihai Donțu
` (2 subsequent siblings)
3 siblings, 0 replies; 11+ messages in thread
From: Segher Boessenkool @ 2007-06-12 16:56 UTC (permalink / raw)
To: gcc-help; +Cc: Mihai Don?u
>> Not that is of great importance, but does anyone know why
>>
>> int main( void )
>> {
>> return printf( "%d\n", ( int )sizeof( main ) );
>> }
>>
>> prints 1 ? :)
> It is invalid to use sizeof() with a function
> type expression (C99 §6.5.3.4 para. 1) and hence this code invokes
> undefined behavior. gcc gives a warning if you compile with -pedantic,
> but otherwise it apparently just returns 1;
See the description of -Wpointer-arith in the Fine Manual.
Segher
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: sizeof( function )
2007-06-12 16:38 ` Brian Dessent
2007-06-12 16:56 ` Segher Boessenkool
@ 2007-06-12 17:05 ` Mihai Donțu
2007-06-12 17:57 ` Ian Lance Taylor
2007-06-14 12:44 ` Martin Krischik
3 siblings, 0 replies; 11+ messages in thread
From: Mihai Donțu @ 2007-06-12 17:05 UTC (permalink / raw)
To: Brian Dessent; +Cc: gcc-help
On Tuesday 12 June 2007 19:28, Brian Dessent wrote:
> Mihai Don?u wrote:
>
> > Not that is of great importance, but does anyone know why
> >
> > int main( void )
> > {
> > return printf( "%d\n", ( int )sizeof( main ) );
> > }
> >
> > prints 1 ? :)
> >
> > (sizeof( &main ) prints 4/8 as it should)
>
> In this context 'main' is a function type, i.e. a type that describes a
> function of a given return type and a given number and type of arguments
> (C99 §6.2.5 para. 20). It is invalid to use sizeof() with a function
> type expression (C99 §6.5.3.4 para. 1) and hence this code invokes
> undefined behavior. gcc gives a warning if you compile with -pedantic,
> but otherwise it apparently just returns 1; it would be equally within
> its rights to return 42 or to generate code to format the users hard
> drive -- that is the nature of undefined behavior.
Unlike me, you really do know your C99 book :)
Thanks a lot!
--
Mihai DonÈu
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: sizeof( function )
2007-06-12 16:38 ` Brian Dessent
2007-06-12 16:56 ` Segher Boessenkool
2007-06-12 17:05 ` Mihai Donțu
@ 2007-06-12 17:57 ` Ian Lance Taylor
2007-06-14 12:44 ` Martin Krischik
3 siblings, 0 replies; 11+ messages in thread
From: Ian Lance Taylor @ 2007-06-12 17:57 UTC (permalink / raw)
To: gcc-help; +Cc: Mihai Don?u
Brian Dessent <brian@dessent.net> writes:
> Mihai Don?u wrote:
>
> > Not that is of great importance, but does anyone know why
> >
> > int main( void )
> > {
> > return printf( "%d\n", ( int )sizeof( main ) );
> > }
> >
> > prints 1 ? :)
> >
> > (sizeof( &main ) prints 4/8 as it should)
>
> In this context 'main' is a function type, i.e. a type that describes a
> function of a given return type and a given number and type of arguments
> (C99 §6.2.5 para. 20). It is invalid to use sizeof() with a function
> type expression (C99 §6.5.3.4 para. 1) and hence this code invokes
> undefined behavior. gcc gives a warning if you compile with -pedantic,
> but otherwise it apparently just returns 1; it would be equally within
> its rights to return 42 or to generate code to format the users hard
> drive -- that is the nature of undefined behavior.
Believe it or not, this is a documented gcc extension.
http://gcc.gnu.org/onlinedocs/gcc-4.2.0/gcc/Pointer-Arith.html
Ian
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: sizeof( function )
2007-06-12 16:38 ` Brian Dessent
` (2 preceding siblings ...)
2007-06-12 17:57 ` Ian Lance Taylor
@ 2007-06-14 12:44 ` Martin Krischik
3 siblings, 0 replies; 11+ messages in thread
From: Martin Krischik @ 2007-06-14 12:44 UTC (permalink / raw)
To: gcc-help
Am 12.06.2007, 19:28 Uhr, schrieb Brian Dessent <brian@dessent.net>:
> Mihai Don?u wrote:
>
>> Not that is of great importance, but does anyone know why
>>
>> int main( void )
>> {
>> return printf( "%d\n", ( int )sizeof( main ) );
>> }
>>
>> prints 1 ? :)
>>
>> (sizeof( &main ) prints 4/8 as it should)
>
> In this context 'main' is a function type, i.e. a type that describes a
> function of a given return type and a given number and type of arguments
> (C99 §6.2.5 para. 20). It is invalid to use sizeof() with a function
> type expression (C99 §6.5.3.4 para. 1) and hence this code invokes
> undefined behavior. gcc gives a warning if you compile with -pedantic,
> but otherwise it apparently just returns 1; it would be equally within
> its rights to return 42 or to generate code to format the users hard
> drive -- that is the nature of undefined behavior.
If it is invalid to use sizeof() with a function type expression - why not
an make it an compile time error?
Martin
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: sizeof( function )
@ 2007-06-14 13:14 Nick Maclaren
0 siblings, 0 replies; 11+ messages in thread
From: Nick Maclaren @ 2007-06-14 13:14 UTC (permalink / raw)
To: gcc-help
"Martin Krischik" <krischik@users.sourceforge.net> wrote:
>
> If it is invalid to use sizeof() with a function type expression - why not
> an make it an compile time error?
That is what most compilers do. However, gcc allows pointer arithmetic
on function pointers, to enable C code to poke through compiled code
(e.g. to disassemble code). Given how few programmers ever do that
sort of thing, it could perfectly well issue a warning even without
-pedantic. But it doesn't.
Note that, for C language reasons, it is virtually impossible to
separate the handling of sizeof(<type>) from allowing arithmetic on
values of type '<type> *'.
Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1@cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: sizeof( function )
@ 2007-06-12 21:55 Nick Maclaren
0 siblings, 0 replies; 11+ messages in thread
From: Nick Maclaren @ 2007-06-12 21:55 UTC (permalink / raw)
To: gcc-help
I stand corrected. When I last looked at the language accepted in
default (not -pedantic) mode, I didn't spot that one. Sorry for
maligning gcc :-)
But, yes, I agree about using -pedantic. Unless you have a real need
for gcc-specific extensions, compiling with that is always the Right
Thing To Do. And -Wall and possibly -Wextra and ....
Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1@cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: sizeof( function )
2007-06-12 17:18 Nick Maclaren
2007-06-12 17:27 ` Brian Dessent
@ 2007-06-12 17:47 ` Segher Boessenkool
1 sibling, 0 replies; 11+ messages in thread
From: Segher Boessenkool @ 2007-06-12 17:47 UTC (permalink / raw)
To: Nick Maclaren; +Cc: gcc-help
>> It is invalid to use sizeof() with a function
>> type expression (C99 '6.5.3.4 para. 1) and hence this code invokes
>> undefined behavior. gcc gives a warning if you compile with
>> -pedantic,
>> but otherwise it apparently just returns 1;
> But this is actually a BUG in gcc - look at 5.1.1.3. The restriction
> that sizeof may not have an argument that is function type is a
> constraint, and therefore a breach requires a diagnostic.
There is no bug, as GCC prints a diagnostic just fine.
That's what -pedantic is for.
Segher
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: sizeof( function )
2007-06-12 17:18 Nick Maclaren
@ 2007-06-12 17:27 ` Brian Dessent
2007-06-12 17:47 ` Segher Boessenkool
1 sibling, 0 replies; 11+ messages in thread
From: Brian Dessent @ 2007-06-12 17:27 UTC (permalink / raw)
To: Nick Maclaren; +Cc: gcc-help
Nick Maclaren wrote:
> But this is actually a BUG in gcc - look at 5.1.1.3. The restriction
> that sizeof may not have an argument that is function type is a
> constraint, and therefore a breach requires a diagnostic.
It would only be a bug if there was no diagnostic in non-GNU mode, e.g.
-std=c89. Otherwise the default is -std=gnu89 if not specified and as
the other poster pointed out, GNU extensions allow this and define the
return value as 1 for convenience.
Brian
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: sizeof( function )
@ 2007-06-12 17:18 Nick Maclaren
2007-06-12 17:27 ` Brian Dessent
2007-06-12 17:47 ` Segher Boessenkool
0 siblings, 2 replies; 11+ messages in thread
From: Nick Maclaren @ 2007-06-12 17:18 UTC (permalink / raw)
To: gcc-help
> It is invalid to use sizeof() with a function
> type expression (C99 '6.5.3.4 para. 1) and hence this code invokes
> undefined behavior. gcc gives a warning if you compile with -pedantic,
> but otherwise it apparently just returns 1;
It's not quite that simple - look at footnote 84 :-(
Both function designators ('function names') and sizeof are weird,
in that they look like a value and a function, but aren't quite.
But this is actually a BUG in gcc - look at 5.1.1.3. The restriction
that sizeof may not have an argument that is function type is a
constraint, and therefore a breach requires a diagnostic.
Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1@cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2007-06-14 10:54 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-12 16:12 sizeof( function ) Mihai Donțu
2007-06-12 16:38 ` Brian Dessent
2007-06-12 16:56 ` Segher Boessenkool
2007-06-12 17:05 ` Mihai Donțu
2007-06-12 17:57 ` Ian Lance Taylor
2007-06-14 12:44 ` Martin Krischik
2007-06-12 17:18 Nick Maclaren
2007-06-12 17:27 ` Brian Dessent
2007-06-12 17:47 ` Segher Boessenkool
2007-06-12 21:55 Nick Maclaren
2007-06-14 13:14 Nick Maclaren
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).