When we check the size of a function using sizeof()
, we always get 1 byte.
What does this 1 byte signify?
This is not undefined behavior - the C language standard requires a diagnostic when using the sizeof
operator with a function designator (a function name) since it's a constraint violation for the sizeof
operator.
However, as an extension to the C language, GCC allows arithmetic on void
pointers and function pointers, which is done by treating the size of a void
or a function as 1
. As a consequence, the sizeof
operator will evaluate to 1
for void
or a function with GCC. See http://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html#Pointer-Arith
You can get GCC to issue a warning when using sizeof
with these operands by using the -pedantic
or -Wpointer-arith
options to GCC. Or make it an error with -Werror=pointer-arith
.