public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* 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
* 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
* 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

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 17:18 sizeof( function ) Nick Maclaren
2007-06-12 17:27 ` Brian Dessent
2007-06-12 17:47 ` Segher Boessenkool
  -- strict thread matches above, loose matches on Subject: below --
2007-06-14 13:14 Nick Maclaren
2007-06-12 21:55 Nick Maclaren
2007-06-12 16:12 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

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).