在使用功能的sizeof输出 [英] Output of using sizeof on a function
问题描述
为什么以下code请提供:
#包括LT&;&stdio.h中GT;INT VOO()
{
的printf(有些code);
返回0;
}
诠释主(){
的printf(%俎,sizeof的VOO);
返回0;
}
下面的输出:
1
C语言没有定义的sizeof
的功能。这位前pression 的sizeof VOO
违反了约束,并且需要从诊断符合任何C编译器。
GCC实现的函数指针作为扩展指针运算。为了支持这一点,GCC任意假设一个函数的大小为1,因此,添加,比如说,42函数的地址给你一个地址42字节超出该函数的地址。
他们做同样的事情无效,因此的sizeof(无效)
在产生1和指针运算无效*
是允许的。
这两种功能都最好避免。使用 -ansi -pedantic
或 -std = C99 -pedantic
来获得这种事情的警告。
Why does the following code give:
#include<stdio.h>
int voo()
{
printf ("Some Code");
return 0;
}
int main() {
printf ("%zu", sizeof voo);
return 0;
}
The following output:
1
The C language does not define sizeof
for functions. The expression sizeof voo
violates a constraint, and requires a diagnostic from any conforming C compiler.
gcc implements pointer arithmetic on function pointers as an extension. To support this, gcc arbitrarily assumes that the size of a function is 1, so that adding, say, 42 to the address of a function will give you an address 42 bytes beyond the function's address.
They did the same thing for void, so sizeof (void)
yields 1, and pointer arithmetic on void*
is permitted.
Both features are best avoided if you want to write portable code. Use -ansi -pedantic
or -std=c99 -pedantic
to get warnings for this kind of thing.
这篇关于在使用功能的sizeof输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!