user-0 提问: sizeof一个函数,大小为什么是1

回复:
user: user-0
content: 引用 10 楼 zhao4zhong1 的回复:http://bbs.chinaunix.net/thread-2021216-1-1.html 懂了, sizeof根据某类型对应的指针类型加1的表现, 决定该类型的大小, 函数指针加1结果比原来就多1, 所以sizeof函数结果为1.

user: user-1
content: http://bbs.chinaunix.net/thread-2021216-1-1.html

user: user-2
content: 我在VS下是提示错误的，sizeof不能是函数，干嘛要去求函数的长度呢，这就像函数是不能搞一个函数数组

user: user-1
content: 引用 4 楼 ipqtjmqj 的回复:[quote=引用 2 楼 zhao4zhong1 的回复:] 参考gcc或g++中sizeof的实现。 sizeof是运算符不是函数,所以在头文件中应该找不到, gcc是开源的, 不过我不知道在哪找它的源码, 特别是要找到sizeof运算符的源码, 麻烦赵4老师给个链接.[/quote] 学会使用百度和grep

user: user-3
content: 引用 6 楼 ipqtjmqj 的回复:[quote=引用 1 楼 user-3 的回复:] C standard N1570 6.5.3.4 The sizeof and _Alignof operators Constraints 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. 你这有_Alignof应该是c11标准吧, 以前的标准是支持的, 编译也没报错, 请问以前的标准怎么说的.[/quote]  ISO/IEC 9899:1999 (E) ©ISO/IEC 6.5.3.4 The sizeof operator Constraints 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.  C89 (ISO/IEC 9899:1990)我从来没有见过，非常抱歉。  但是从 http://en.cppreference.com/w/c/language/sizeof 的叙述来看，sizeof 的这一要求从 C89 以来没有发生变化。这个网站对在某版标准开始发生变化的内容都有特别的标注。

user: user-0
content: 引用 3 楼 user-3 的回复: $ cat test.c #include <stdio.h> typedef int f(int, ...); int main() {     printf("%d,%d", sizeof(printf), sizeof(f));     return 0; } $ gcc --std=c11 --pedantic-errors test.c test.c: In function ‘main’: test.c:5:27: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]      printf("%d,%d", sizeof(printf), sizeof(f));                            ^ test.c:5:44: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]      printf("%d,%d", sizeof(printf), sizeof(f));                                             ^ test.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]      printf("%d,%d", sizeof(printf), sizeof(f));      ^ test.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat=] $ gcc --version gcc (Ubuntu 4.9.2-10ubuntu13) 4.9.2 Copyright (C) 2014 Free Software Foundation, Inc. This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  你这是c++11吧, 报错了, 用以前的标准不会报错.

user: user-0
content: 引用 1 楼 user-3 的回复:C standard N1570 6.5.3.4 The sizeof and _Alignof operators Constraints 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. 你这有_Alignof应该是c11标准吧, 以前的标准是支持的, 编译也没报错, 请问以前的标准怎么说的.

user: user-0
content: 引用 2 楼 zhao4zhong1 的回复:参考gcc或g++中sizeof的实现。 sizeof是运算符不是函数,所以在头文件中应该找不到, gcc是开源的, 不过我不知道在哪找它的源码, 特别是要找到sizeof运算符的源码, 麻烦赵4老师给个链接.

user: user-3
content: $ cat test.c #include <stdio.h> typedef int f(int, ...); int main() {     printf("%d,%d", sizeof(printf), sizeof(f));     return 0; } $ gcc --std=c11 --pedantic-errors test.c test.c: In function ‘main’: test.c:5:27: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]      printf("%d,%d", sizeof(printf), sizeof(f));                            ^ test.c:5:44: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]      printf("%d,%d", sizeof(printf), sizeof(f));                                             ^ test.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]      printf("%d,%d", sizeof(printf), sizeof(f));      ^ test.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat=] $ gcc --version gcc (Ubuntu 4.9.2-10ubuntu13) 4.9.2 Copyright (C) 2014 Free Software Foundation, Inc. This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

user: user-1
content: 参考gcc或g++中sizeof的实现。

user: user-3
content: C standard N1570 6.5.3.4 The sizeof and _Alignof operators Constraints 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.

user: user-3
content: 引用 11 楼 ipqtjmqj 的回复:[quote=引用 10 楼 zhao4zhong1 的回复:] http://bbs.chinaunix.net/thread-2021216-1-1.html 懂了, sizeof根据某类型对应的指针类型加1的表现, 决定该类型的大小, 函数指针加1结果比原来就多1, 所以sizeof函数结果为1.[/quote]  gcc 对指针运算进行了扩展(GNU C)，允许对 函数指针 进行算数运算（比如 p + 1）。为了支持这种算数运算，才支持 sizeof(function) == 1 。  以下引用自：https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html  6 Extensions to the C Language Family 引用6.23 Arithmetic on void- and Function-Pointers  In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.  A consequence of this is that sizeof is also allowed on void and on function types, and returns 1.  The option -Wpointer-arith requests a warning if these extensions are used.  对 函数指针 进行算数运算，sizeof(function) 在标准里原本都是不允许的。

user: user-3
content: 引用 5 楼 ipqtjmqj 的回复:[quote=引用 3 楼 user-3 的回复:]  $ cat test.c #include <stdio.h> typedef int f(int, ...); int main() {     printf("%d,%d", sizeof(printf), sizeof(f));     return 0; } $ gcc --std=c11 --pedantic-errors test.c test.c: In function ‘main’: test.c:5:27: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]      printf("%d,%d", sizeof(printf), sizeof(f));                            ^ test.c:5:44: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]      printf("%d,%d", sizeof(printf), sizeof(f));                                             ^ test.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]      printf("%d,%d", sizeof(printf), sizeof(f));      ^ test.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat=] $ gcc --version gcc (Ubuntu 4.9.2-10ubuntu13) 4.9.2 Copyright (C) 2014 Free Software Foundation, Inc. This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  你这是c++11吧, 报错了, 用以前的标准不会报错.[/quote]   $ gcc --std=c89 --pedantic-errors test.c test.c: In function ‘main’: test.c:5:27: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]      printf("%d,%d", sizeof(printf), sizeof(f));                            ^ test.c:5:44: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]      printf("%d,%d", sizeof(printf), sizeof(f));                                             ^ test.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]      printf("%d,%d", sizeof(printf), sizeof(f));      ^ test.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat=]