From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3250 invoked by alias); 14 Jun 2007 10:37:36 -0000 Received: (qmail 3229 invoked by uid 22791); 14 Jun 2007 10:37:35 -0000 X-Spam-Check-By: sourceware.org Received: from server265-han.de-nserver.de (HELO server265-han.de-nserver.de) (85.158.176.48) by sourceware.org (qpsmtpd/0.31) with ESMTP; Thu, 14 Jun 2007 10:37:33 +0000 Received: (qmail 1812 invoked from network); 14 Jun 2007 12:37:16 +0200 Received: from Unknown (HELO super) (213.154.202.43) (smtp-auth username martinka, mechanism login) by server265-han.de-nserver.de (qpsmtpd/0.32) with ESMTP; Thu, 14 Jun 2007 12:37:15 +0200 Date: Thu, 14 Jun 2007 12:44:00 -0000 To: gcc-help@gcc.gnu.org Subject: Re: sizeof( function ) From: "Martin Krischik" Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 References: <200706121826.34044.mihai.dontu@gmail.com> <466EC9C6.A5DF578A@dessent.net> Content-Transfer-Encoding: 8bit Message-ID: In-Reply-To: <466EC9C6.A5DF578A@dessent.net> User-Agent: Opera Mail/9.21 (Win32) X-User-Auth: Auth by martinka through 213.154.202.43 X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2007-06/txt/msg00188.txt.bz2 Am 12.06.2007, 19:28 Uhr, schrieb Brian Dessent : > 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