Bug 39134 - front end does not reject sizeof on function types
Summary: front end does not reject sizeof on function types
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2009-02-09 02:06 UTC by Ben Elliston
Modified: 2014-07-20 14:12 UTC (History)
1 user (show)

See Also:
Host: powerpc64-linux
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ben Elliston 2009-02-09 02:06:08 UTC
The following:

#include <stddef.h>

void test(void)
{
        size_t s1 = sizeof(test);
        size_t s2 = sizeof(void (void));
}

should not compile with -std=c99. According to the C99 standard section 6.5.3.4.1 "The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member."

I would expect gcc to produce an error in this case.  sizeof actually
returns 1 and no error.
Comment 1 Andreas Schwab 2009-02-09 09:30:42 UTC
This is a GCC extension, use -Wpointer-arith or -pedantic or -pedantic-errors.

$ gcc -c -std=c99 -pedantic-errors cast.c 
cast.c: In function &#8216;test&#8217;:
cast.c:6: error: invalid application of &#8216;sizeof&#8217; to a function type
cast.c:7: error: invalid application of &#8216;sizeof&#8217; to a function type
Comment 2 Chris Hennick 2014-07-20 14:12:34 UTC
Shouldn't it at least raise a warning, even using the default invocation? To me, a positive sizeof(x) implies that x is an object that can be moved and copied, and from what I've read, some novice programmers seem to expect that to be true for compiled and loaded functions.

Of course, an alternative would be to actually compile/link all sizeof'd functions in such a way that they *could* be moved and copied...