免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5465 | 回复: 8
打印 上一主题 下一主题

[C] sizeof(long(long))这事啥子意思? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-07-08 11:40 |只看该作者 |倒序浏览
sizeof(long(long))这事啥子意思?
求出结果为1
long(long)怎么解释?强制转换么?

论坛徽章:
5
狮子座
日期:2013-08-20 10:12:24午马
日期:2013-11-23 18:04:102015年辞旧岁徽章
日期:2015-03-03 16:54:152015亚冠之德黑兰石油
日期:2015-06-29 18:11:1115-16赛季CBA联赛之新疆
日期:2024-02-21 10:00:53
2 [报告]
发表于 2014-07-08 11:55 |只看该作者
long(long)是函数类型。

函数类型的大小是实现定义的(还是未定义?还是别的什么?)反正意思就是取不到,所以给你返回一个1意思一下。

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:45
3 [报告]
发表于 2014-07-08 15:09 |只看该作者
本帖最后由 koolcoy 于 2014-07-08 15:10 编辑

没记错的话,implementation defined的行为在不同的实现里行为可能不一样,但是在同一个实现里行为必须一样,不过具体啥行为由实现决定,可以当作实现的扩展来用。

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
4 [报告]
发表于 2014-07-11 14:12 |只看该作者
回复 1# zheguzai


    这本来是个错误的语法,是标准所不允许的,但是gcc的扩展允许此用法。

c标准中对sizeof有如下规定: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.

根据上述规定定function type是不被允许的。

gcc的有如下所述的扩展:
5.17 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.
https://gcc.gnu.org/onlinedocs/gcc-4.0.4/gcc/Pointer-Arith.html#Pointer-Arith

可见之所以返回1是由于gcc的扩展影响的结果。

论坛徽章:
9
摩羯座
日期:2013-08-15 15:18:48狮子座
日期:2013-09-12 18:07:47金牛座
日期:2013-09-16 13:23:09辰龙
日期:2013-10-09 09:03:27白羊座
日期:2013-10-17 13:32:44子鼠
日期:2014-04-23 15:09:38戌狗
日期:2014-09-17 11:37:542015年亚洲杯之韩国
日期:2015-03-26 10:16:442015亚冠之武里南联
日期:2015-08-18 14:55:52
5 [报告]
发表于 2014-07-11 14:50 |只看该作者
这个long(long)没见过,说是函数类型,应该怎么理解,貌似没有遇到它的应用场景。
long(*)(long)倒是容易理解,是个函数指针,sizeof应该是4吧,与这个long(long)应该不同。

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
6 [报告]
发表于 2014-07-11 16:42 |只看该作者
回复 5# w_anthony

A function type describes a function with specified return type. A function type is characterized by its return type and the number and types of its parameters. A function type is said to be derived from its return type, and if its return type is T , the function type is sometimes called ‘‘function returning T’’. The construction of a function type from a return type is called ‘‘function type derivation’’.

根据标准的解释,一个函数类型描述了一个函数及其返回值类型,它的特点是具有返回值和参数。由此可知long(long)实际上是一个Type Name,而这个Type Name指向function type。


论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
7 [报告]
发表于 2014-07-18 03:41 |只看该作者
回复 6# myworkstation


    好吧, 语言律师。

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
8 [报告]
发表于 2014-07-18 09:26 |只看该作者
好像C99支持函数类型。以前的不支持吧!

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
9 [报告]
发表于 2014-07-19 15:17 |只看该作者
回复 8# cobras


    支持的,C89就有明确的规定了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP