“sizeof(函数名)”是什么意思?返回?
示例代码:
int main(void)
{
printf ("size = %d\n", sizeof(main));
}
应用于函数名称(例如 main
)的返回值 sizeof
是多少?
Sample code:
int main(void)
{
printf ("size = %d\n", sizeof(main));
}
What is the returned value sizeof
applied to a function name, for example main
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C 标准禁止它 - 当使用 gcc -pedantic 编译时,它会产生
invalid application of 'sizeof' to a function type
警告。然而,
gcc
编译它并为sizeof(main)
返回1
,并且它不是函数指针的大小。它似乎与编译器相关。
C standard forbids it - when compiled with
gcc -pedantic
, it producesinvalid application of ‘sizeof’ to a function type
warning.However
gcc
compiles it and returns1
forsizeof(main)
, and it is not a size of function pointer.It seems to be compiler-dependent.
使用编译标志
-Wall -pedantic
向sizeof
发出有关错误操作数的警告(记住sizeof
是编译时运算符),代码:编译消息使用 GCC 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5):
另请阅读:
此外,
size_t
的正确格式字符串是%zu
,如果它不存在,例如 Microsoft 的编译器,那么您可以使用%lu
和将返回值转换为unsigned long
。Use compilation flag
-Wall -pedantic
to emit warning about incorrect operand tosizeof
(remembersizeof
is a compilation time operator), Code:Compilation message with GCC version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5):
Read also:
Additionally, correct format string for
size_t
is%zu
and If it doesn't exists for example Microsoft's compiler then you can use%lu
and convert the returned value tounsigned long
.ISO C++ 禁止将
sizeof
应用于函数类型的表达式。ISO/IEC 14882 on C++ 规定(第 5.3.3 节):
“大小运算符不得应用于具有函数或不完整类型的表达式,...”
对于标准 C ( ISO/IEC 9899:1999) 第 6.5.3.4 节:
“sizeof 运算符不得应用于具有函数类型或不完整类型的表达式、此类类型的括号名称或指定的表达式位字段成员。”
ISO C++ forbids applying
sizeof
to an expression of function type.ISO/IEC 14882 on C++ says (section 5.3.3):
"The size operator shall not be applied to an expression that has function or incomplete type,..."
The same hold for standard C (ISO/IEC 9899:1999) section 6.5.3.4:
"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."
根据 ISO C11 第
6.5.3.4 The sizeof 和 _Alignof 运算符
第 1 小节(约束):6.3.2.1 左值、数组和函数指示符
中还有第 4 小节,其中指出:引用自那里的脚注 65 澄清了这一点:
根据
4 一致性
部分:因此,严格遵守的程序不应采用函数的大小。但是,话又说回来,它可能还应该使用正确的
main()
形式:-)但是,这里有一个漏洞。允许一致的实现提供扩展“只要它们不改变任何严格的行为”
符合程序”(
4 一致性
部分,6
小节)。您可能会认为这是一种行为改变(允许
sizeof(function )
而不是不允许它)但是,由于原始程序一开始就不会严格遵守,因此本小节并不禁止它。As per ISO C11 section
6.5.3.4 The sizeof and _Alignof operators
, sub-section 1 (constraints):There's also a subsection 4 in section
6.3.2.1 Lvalues, arrays, and function designators
which states:Foot note 65, referenced from there, clarifies the point:
As per section
4 Conformance
:Hence a strictly conforming program should never take the size of a function. But, then again, it probably should also use a correct form of
main()
:-)There is a loophole here, however. A conforming implementation is allowed to provide extensions "provided they do not alter the behavior of any strictly
conforming program" (section
4 Conformance
, subsection6
).You could argue that this is a behavioural change (allowing
sizeof(function)
rather than disallowing it) but, since the original program wouldn't have been strictly conforming in the first place, the subsection does not forbid it.