免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: zmdesperado
打印 上一主题 下一主题

求sizeof(f)大小的疑问 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2011-08-24 19:34 |只看该作者
好像不能对函数名进行sizeof()运算。
haoyuejushi 发表于 2011-08-24 16:35



    函数名确实没有这个运算

论坛徽章:
0
12 [报告]
发表于 2011-08-24 19:35 |只看该作者
C99标准规定,函数、不能确定类型的表达式以及位域(bit-field)成员不能被计算sizeof值;
sizeof是在编译时确定的,不是运行时确定;
...
davidfoxhu 发表于 2011-08-24 17:12

对VLA,sizeof不是编译时确定的

论坛徽章:
1
技术图书徽章
日期:2014-07-11 16:30:58
13 [报告]
发表于 2011-08-24 20:22 |只看该作者
#define   sizeof(x)  ({   typeof(x)   _x; (char   *)((&_x+1))   -   (char   *)(&_x);})

是一个宏,不是函数。   

哥哥们呀 还看汇编,唉。。。

论坛徽章:
11
未羊
日期:2013-12-16 12:45:4615-16赛季CBA联赛之青岛
日期:2016-04-11 19:17:4715-16赛季CBA联赛之广夏
日期:2016-04-06 16:34:012015亚冠之卡尔希纳萨夫
日期:2015-11-10 10:04:522015亚冠之大阪钢巴
日期:2015-07-30 18:29:402015亚冠之城南
日期:2015-06-15 17:56:392015亚冠之卡尔希纳萨夫
日期:2015-05-15 15:19:272015亚冠之山东鲁能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16赛季CBA联赛之八一
日期:2016-07-22 09:41:40
14 [报告]
发表于 2011-08-24 20:51 |只看该作者
#define   sizeof(x)  ({   typeof(x)   _x; (char   *)((&_x+1))   -   (char   *)(&_x);})

是一个宏, ...
manULinux 发表于 2011-08-24 20:22



    typeod(f) 是什么呢

论坛徽章:
0
15 [报告]
发表于 2011-08-24 20:56 |只看该作者
本帖最后由 幻の上帝 于 2011-08-24 21:01 编辑

Undefined behavior.
ISO C99:
4/2 If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint is violated, the behavior is undefined. Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior. There is no difference in emphasis among these three; they all describe ‘‘behavior that is undefined’’.
6.3.2.1/4
A function designator is an expression that has function type. Except when it is the operand of the sizeof operator 54) or the unary & operator, a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’.
54) Because this conversion does not occur, the operand of the sizeof operator remains a function designator and violates the constraint in 6.5.3.4.
6.5.3.4/2 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 lvalue that designates a bit-field object.
PS.C89也一样。
PS2.C++直接ill-formed了所以报错。
PS3.至今买不起。

论坛徽章:
0
16 [报告]
发表于 2011-08-24 21:02 |只看该作者
#define   sizeof(x)  ({   typeof(x)   _x; (char   *)((&_x+1))   -   (char   *)(&_x);})

是一个宏, ...
manULinux 发表于 2011-08-24 20:22


原来关键字是宏……

论坛徽章:
1
技术图书徽章
日期:2014-07-11 16:30:58
17 [报告]
发表于 2011-08-25 08:13 |只看该作者
回复 14# zylthinking


    重新温习一下C吧 。

论坛徽章:
1
技术图书徽章
日期:2014-07-11 16:30:58
18 [报告]
发表于 2011-08-25 08:24 |只看该作者
回复 16# 幻の上帝


    我没说他不是关键字,实现方式不是宏么?

论坛徽章:
11
未羊
日期:2013-12-16 12:45:4615-16赛季CBA联赛之青岛
日期:2016-04-11 19:17:4715-16赛季CBA联赛之广夏
日期:2016-04-06 16:34:012015亚冠之卡尔希纳萨夫
日期:2015-11-10 10:04:522015亚冠之大阪钢巴
日期:2015-07-30 18:29:402015亚冠之城南
日期:2015-06-15 17:56:392015亚冠之卡尔希纳萨夫
日期:2015-05-15 15:19:272015亚冠之山东鲁能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16赛季CBA联赛之八一
日期:2016-07-22 09:41:40
19 [报告]
发表于 2011-08-25 10:27 |只看该作者
回复  zylthinking


    重新温习一下C吧 。
manULinux 发表于 2011-08-25 08:13


授我以鱼, 不要授我以渔

论坛徽章:
0
20 [报告]
发表于 2011-08-25 11:02 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP