免费注册 查看新帖 |

Chinaunix

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

[C] 判断是16位机还是32位机,不用sizeof [复制链接]

论坛徽章:
0
31 [报告]
发表于 2007-12-02 14:31 |只看该作者
原帖由 Edengundam 于 2007-11-29 09:45 发表



C99标准允许对指针进行加法和减法的运算.

6.5.6 Additive operators

When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the point ...


>> C99标准允许对指针进行加法和减法的运算.

这固然不错。但是不能据此说指针运算的结果一定是正确的。

>> When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand.

就像整数除以整数的结果仍然是整数类型一样,这并不是说除数为 0 的情况下结果是有定义的。

>> 关于指向数组成员的指针来说, 去解引用才受到限制.

解引用固然受此限制,那么引文中“otherwise, the behavior is undefined.” 中的“otherwise” 指的情况呢?不正是我在上面提到的情况吗?

论坛徽章:
0
32 [报告]
发表于 2007-12-02 15:13 |只看该作者
原帖由 whyglinux 于 2007-12-2 14:31 发表


>> 关于指向数组成员的指针来说, 去解引用才受到限制.

解引用固然受此限制,那么引文中“otherwise, the behavior is undefined.” 中的“otherwise” 指的情况呢?不正是我在上面提到的情况吗?

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.


红色这句是上下文. 根本没有提到如果指针不是这种情况下的定义.  脱离这句话otherwise就没意义了, 我没有看到其他地方关于在指向任意地址上进行加减操作的定义, 既没有规定"未定义", 也没有"非法".

论坛徽章:
0
33 [报告]
发表于 2007-12-02 15:22 |只看该作者
原帖由 Edengundam 于 2007-12-2 15:13 发表


红色这句是上下文. 根本没有提到如果指针不是这种情况下的定义.  脱离这句话otherwise就没意义了, 我没有看到其他地方关于在指向任意地址上进行加减操作的定义, 既没有规定"未定义", 也没有"非法".


如果不好理解的话,建议看看 C99 标准的 J.2 Undefined behavior 的下面这句话,说得更明白些:

(The behavior is undefined in the following circumstances

— Addition or subtraction of a pointer into, or just beyond, an array object and an
integer type produces a result that does not point into, or just beyond, the same array
object (6.5.6).

论坛徽章:
0
34 [报告]
发表于 2007-12-02 15:35 |只看该作者
原帖由 Edengundam 于 2007-11-29 09:45 发表



关于指向数组成员的指针来说, 去解引用才受到限制.
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.
...


这里其实没有提到解引用。这里的  ``the evaluation shall ...'' 指的是对指针加法运算的 evaluation.

论坛徽章:
0
35 [报告]
发表于 2007-12-02 15:51 |只看该作者
原帖由 win_hate 于 2007-12-2 15:35 发表


这里其实没有提到解引用。这里的  ``the evaluation shall ...'' 指的是对指针加法运算的 evaluation.


对这句:
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.

论坛徽章:
0
36 [报告]
发表于 2007-12-02 15:54 |只看该作者
原帖由 whyglinux 于 2007-12-2 15:22 发表


如果不好理解的话,建议看看 C99 标准的 J.2 Undefined behavior 的下面这句话,说得更明白些:

(The behavior is undefined in the following circumstances

— Addition or subtraction of a pointer into, or just beyond, an array object and an
integer type produces a result that does not point into, or just beyond, the same array
object (6.5.6).




这句话依然是说:
指向或刚好超出数组对象的指针与整数类型进行加法或减法产生的结果没有指向或刚好超过同一个数组对象

反过来, 这里没有定义, 当指针没有指向数组对象时的行为.

论坛徽章:
0
37 [报告]
发表于 2007-12-02 16:20 |只看该作者
原帖由 Edengundam 于 2007-12-2 15:51 发表


对这句:
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.


这个是说,尽管可以越雷池一步---这个指针运算是合法的,但越了界的指针不能解引用。从上下文看,这里的 evaluated 指的是 *,但之前的指的却是加法运算。

你想,如果不是这样理解,那么为什么非得有 ``one past the last element'' 的限制呢?

论坛徽章:
0
38 [报告]
发表于 2007-12-02 16:24 |只看该作者
原帖由 win_hate 于 2007-12-2 16:20 发表


这个是说,尽管可以越雷池一步---这个指针运算是合法的,但越了界的指针不能解引用。从上下文看,这里的 evaluated 指的是 *,但之前的指的却是加法运算。

你想,如果不是这样理解,那么为什么非得有 ...



没错, 但是这些都是特指数组对象的指针.
如果指针没有指向数组对象, 这些规则都不管用.

论坛徽章:
0
39 [报告]
发表于 2007-12-02 16:25 |只看该作者
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.

上面说的是 P + N 情况。这里 P 是指针,N 是整数。上文翻译如下:

如果指针操作数(P)和结果(P + N)都指向同一数组对象的元素或者(不是数组元素的)次数组对象最后一个元素的下一个位置,(对 P + N 的)求值不应产生溢出;否则,其行为是未定义的。如果结果(P + N)指向数组对象最后一个元素的下一个位置,它不应该用作用来进行求值的单目运算符 * 的操作数。

----------------------------------------

(The behavior is undefined in the following circumstances:)

— Addition or subtraction of a pointer into, or just beyond, an array object and an
integer type produces a result that does not point into, or just beyond, the same array
object (6.5.6).

翻译如下:

(下列情况属于未定义行为:)

— 如果一个指针指向数组对象或者数组对象最后一个元素的下一个位置,而对此指针加上或者减去一个整数类型产生的结果既不指向同一数组数组对象,也不指向(同一数组对象的)最后一个元素的下一个位置

论坛徽章:
0
40 [报告]
发表于 2007-12-02 16:28 |只看该作者
我这里有一本 ``C---A reference Manual'',可以看作简版的标准。

7.6 Binary Operator Expressions

p229  最下面讲:

When adding a pointer p and an integer k, it is assumed that the object that p pints
to lies with in an array of such objects
or is one object beyond the last object in the
array, and the result is a pointer to that object within (or just after) the presumed
array that lies k objects away form the one p p points to. For example, p+1 points
to the object just after one p points to , and p+(-1) points to the object just before.
If the pointers p or p+k do not lie within (or just after) the array, then the behvior
is undefined.


[ 本帖最后由 win_hate 于 2007-12-2 16:29 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP