Why is the size of a function in C always 1 byte?

后端 未结 4 1664
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 00:14

When we check the size of a function using sizeof(), we always get 1 byte. What does this 1 byte signify?

4条回答
  •  迷失自我
    2020-11-30 00:52

    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.

提交回复
热议问题