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.
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 ‘test’: cast.c:6: error: invalid application of ‘sizeof’ to a function type cast.c:7: error: invalid application of ‘sizeof’ to a function type
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...