This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: sizeof( function )


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]