sizeof(函数名)

iamliadai 2008-09-08 08:26:20
#include<stdio.h>
int main(void)
{
printf("%d",sizeof(main)); //结果为 1,这样做有什么意义?
return 0;
}
...全文
632 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
lisa204 2011-09-27
  • 打赏
  • 举报
回复
vc6.0不通过
星羽 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 djwinter 的回复:]
Chiyer:
不知道你在哪个编译器尝试过?

我根本不认为这些代码会像想象那样工作


sizeof(main())
sizeof(&main)
是正确是使用,一般的编译器都应该支持的



sizeof(main) 则是不正确的
[/Quote]
djwinter 2008-09-09
  • 打赏
  • 举报
回复
Chiyer:
不知道你在哪个编译器尝试过?

我根本不认为这些代码会像想象那样工作
星羽 2008-09-09
  • 打赏
  • 举报
回复
The operand to sizeof can be one of the following:

A type name. To use sizeof with a type name, the name must be enclosed in parentheses.

An expression. When used with an expression, sizeof can be specified with or without the parentheses. The expression is not evaluated.
星羽 2008-09-09
  • 打赏
  • 举报
回复

int main()
{
printf("%u\n", sizeof(main())); // 函数返回值类型大小
printf("%u\n", sizeof(&main)); // 函数指针大小
return 0;

}
djwinter 2008-09-09
  • 打赏
  • 举报
回复
函数返回的是指针
sizeof我想是指针类型大小

但是很多编译器应该不让编译通过:)
waguju 2008-09-09
  • 打赏
  • 举报
回复
unix/cc编译不通过:error:cann't take sizeof function:main
hyyuanqiang 2008-09-09
  • 打赏
  • 举报
回复
理论上说 应该是4 吧????


谢谢楼主,支持分享。
家有萌宝V 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 k2eats 的回复:]
The sizeof operator cannot be used with the following operands:
• Functions. (However, sizeof can be applied to pointers to functions.)
• Bit fields.
• Undefined classes.
• The type void.
• Dynamically allocated arrays.
比如使用malloc的只会计算出指针大小
• External arrays.
extern char a[];
外部的数组的尺寸无法sizeof
• Incomplete types.
• Parenthesized names of incom…
[/Quote]
太乙 2008-09-09
  • 打赏
  • 举报
回复
汗~~讨论这啊?

不过学习了还是!
puzzlesky 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 Chiyer 的回复:]
C/C++ code
int main()
{
printf("%u\n", sizeof(main())); // 函数返回值类型大小
printf("%u\n", sizeof(&main)); // 函数指针大小
return 0;

}
[/Quote]

不错
mifeixq 2008-09-09
  • 打赏
  • 举报
回复
用gcc编译通过了……
结果为1。

这是相应的汇编……
cat test.s
.file "test.c"
.section .rodata
.LC0:
.string "%d"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $20, %esp
movl $1, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
addl $20, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)"
.section .note.GNU-stack,"",@progbits

xkyx_cn 2008-09-09
  • 打赏
  • 举报
回复
楼主,想办法看看汇编,不过sizeof是编译时的,所以可能看不到什么有用的

可能你的编译器做了特殊的处理
柯本 2008-09-09
  • 打赏
  • 举报
回复
#include <stdio.h>
test()
{
}
int main(void)
{
printf("%d %d",sizeof(main),sizeof(main)); //结果为 1,这样做有什么意义?
return 0;
}
用gcc的c编译器可以编译通过,结果是为1
估计是个BUG
如果是函数地址,它应该为4
其它编译器(BCB或VC)无法编译
qinqinhao 2008-09-09
  • 打赏
  • 举报
回复
ding
dch4890164 2008-09-09
  • 打赏
  • 举报
回复
这个你的想法挺让我意外的,但是好像没有什么实际意义的吧!
lbh2001 2008-09-09
  • 打赏
  • 举报
回复
呵呵,只能说这是不符合标准的,标准未定义的行为,不同的编译器都可以采取不同的做法
其他的例子还有

sizeof(void);在标准中是不允许的
但实际上
sizeof(void)==0 in VC++6.0
sizeof(void)==1 in GCC3.1
xuxingok 2008-09-09
  • 打赏
  • 举报
回复
。。。这个对函数没什么意义吧
hackxq 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zmlovelx 的回复:]
vc6.0
error C2070: illegal sizeof operand
gcc
error: ISO C++ forbids applying `sizeof' to an expression of
[/Quote]

很奇怪啊.我用MinGW_5.1.4在WINXP下用gcc就可以正确编译并且运行楼主的代码.sizeof(main)的结果就是"1"!
Promi 2008-09-09
  • 打赏
  • 举报
回复
main不是一个函数地址
而是一个标号
就好比汇编中一般约定的start:一样。
之所以这样,我猜测是为了实现main的递归调用。

就好比一个空类定义的对象sizoof运算的结果也是1。
加载更多回复(18)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧