免费注册 查看新帖 |

Chinaunix

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

[C] 有多少C程序员认为这是错误的写法 [复制链接]

论坛徽章:
0
421 [报告]
发表于 2012-09-04 17:55 |只看该作者
回复 420# pmerofc


   
6.5.6.8

When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of  the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the  behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.


再贴一段
6.2.6.1.4

Values stored in non-bit-field objects of any other object type consist of n × CHAR_BIT bits, where n is the size of an object of that type, in bytes. The value may be copied into an object of type unsigned char [n] (e.g., by memcpy); the resulting set of bytes is called the object representation of the value. Values stored in bit-fields consist of m bits, where m is the size specified for the bit-field. The object representation is the set of m bits the bit-field comprises in the addressable storage unit holding it. Two values (other than NaNs) with the same object representation compare equal, but values that compare equal may have different object representations.


就是说, 把 array 看成是普通 object, 将 objecttype* 转换为 unsigned char* (关于 void*, char*, unsigned char* 就不多说了, 见楼上) 并用 unsgiend char * 作为 + 的 operand 是可以正常定位的. 感觉标准在这里为了尽量不干扰指针的概念, 对此说的不清楚, 但通过对 malloc 的态度 (The pointer returned if the allocation
succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated).) 以及"连续"(contiguously)等若有若无的概念 可以稍作佐证.

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
422 [报告]
发表于 2012-09-04 19:54 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
11
摩羯座
日期:2013-09-16 11:10:272015亚冠之阿尔萨德
日期:2015-06-12 22:53:29午马
日期:2014-04-15 11:08:53亥猪
日期:2014-03-02 23:46:35申猴
日期:2013-12-06 22:07:00亥猪
日期:2013-11-28 12:03:13双鱼座
日期:2013-11-21 14:43:56亥猪
日期:2013-10-23 10:55:49处女座
日期:2013-10-17 18:15:43午马
日期:2013-09-27 17:40:4215-16赛季CBA联赛之青岛
日期:2016-06-22 00:45:55
423 [报告]
发表于 2012-09-04 20:30 |只看该作者
本帖最后由 Ager 于 2012-09-04 20:41 编辑

@pmerofc

@hbmhalley


pmerofc 发表于 2012-09-04 19:54
  就是这里
   

    If the pointer operand points to an element of an array object,



     这句话应该是指针加法的一个前提条件,对于二维数组,这里的element应该是指的是某个一维数组



实在有点不好意思 …… 我有点糊涂了 ……

“ If the pointer operand points to an element of an array object ...” 这句话应该不被 pscore + 5 * i + j 这个表达式所适用吧?

pscore是一个“points to an element of an array object”的operand吗?

pscore只是一个float *,它怎么知道自己“points to an element of an array object”

糊涂了,请大虾们多提醒提醒 ……





论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
424 [报告]
发表于 2012-09-04 20:42 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
11
摩羯座
日期:2013-09-16 11:10:272015亚冠之阿尔萨德
日期:2015-06-12 22:53:29午马
日期:2014-04-15 11:08:53亥猪
日期:2014-03-02 23:46:35申猴
日期:2013-12-06 22:07:00亥猪
日期:2013-11-28 12:03:13双鱼座
日期:2013-11-21 14:43:56亥猪
日期:2013-10-23 10:55:49处女座
日期:2013-10-17 18:15:43午马
日期:2013-09-27 17:40:4215-16赛季CBA联赛之青岛
日期:2016-06-22 00:45:55
425 [报告]
发表于 2012-09-04 21:10 |只看该作者
本帖最后由 Ager 于 2012-09-04 21:20 编辑

@pmerofc

pmerofc 发表于 2012-09-04 20:42
这个pscore指向的是一维数组的元素


与其说“pscore指向的是一维数组的元素”,不如说:“pscore指向的是一个float元素” —— 元素在这里,只是在array这个观念下才呈现出来的观念 —— 所以,更不如说:

pscore指向的是一个float。(这就是它在被定义的时候,编译器赋予它的本真含义。)

我所感兴趣的关键,是:

float * pscore = * score;  ——  编译器能否透过这么一个初始化式,将“array”及由“array”所产生的“element”这些属性(或信息),也加赋到pscore身上?

我的理解是:在上述初始化式中,编译器无非是把一个float *的数据值(这个数据值,可以由任何表达式得到,只要类型无二致),单纯地copy给了pscore变量。请允许我这么说。

编译器除了检查 * score 这个表达式的类型是否符合初始化等号左边的pscore的类型(即float *)(显然符合)之外,pscore 并不会“被知道”它自己should be a pointer points 二维数组的元素的元素,还是三维数组的元素的元素的元素,还是四维数组的元素的元素的元素的元素,还是压根儿就没有数组的单独一个float。

{:3_185:}





论坛徽章:
0
426 [报告]
发表于 2012-09-04 21:43 |只看该作者
本帖最后由 hbmhalley 于 2012-09-04 21:45 编辑

回复 422# pmerofc


   
The value may be copied into an object of type unsigned char [n] (e.g., by memcpy)

    没错, 但此处超越了指针的类型.

    换句话说,
这句话应该是指针加法的一个前提条件,对于二维数组,这里的element应该是指的是某个一维数组

    不管是几维数组, 它都是一个 object, 就都可以用 unsigned char * 遍历其中内容

    没记错的话, 原问题除了 overflow 再没有其它争议了吧

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
427 [报告]
发表于 2012-09-04 21:45 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
428 [报告]
发表于 2012-09-04 21:52 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
429 [报告]
发表于 2012-09-04 21:56 |只看该作者
回复 425# Ager


    意思应该不是说 指向数组元素的 pscore 一定有什么特殊的表示, 而是 保证不出现问题 iff pscore 指向 float 数组内元素. 至于违规到底会出现什么问题, 不知道, 但已明确是 undefined.
    反正不是个编译期的问题

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
430 [报告]
发表于 2012-09-04 22:55 |只看该作者
回复 395# Ager

幸好我有拍照过,就p[x]与*(p+x)这事的来龙去脉在 104# 楼。

Array subscripting, Semantics

A postfix expression followed by an expression in square brackets [] is a subscripted designation of a member of an array object.
The definition of the subscript operator [] is that E1[E2] is identical to (*(E1+(E2))) .

p[x]就是根据*(p+x)定义的。

你遇见过"完全相同的表达式被编译为不同的汇编代码"这种情况没有?
按你的说法,因为它们产生的汇编代码不同,所以两个表达式是不等价的?即使两个表达式完全相同?荒唐。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP