C 标准是否要求指针为(整数)数字?
Does the C standard require pointers to be (integer) numbers?
C 标准是否要求指针为(整数)数字?
有人可能会说是的,因为指针算法...
但另一方面,像--
或++
这样的操作可以理解为上一个内存位置、下一个内存位置,取决于它们在标准中的描述方式,实际实现可能使用任何表示来保存指针数据(只要实现了提到的操作)...
想到另一个问题 - C 是否要求 arrays/buffers 等连续,即下一个元素存储在 下一个内存位置(++p
其中 p 是一个指针)?我问是因为您经常可以在网上看到似乎假设它确实如此的实现。
不,指针不必是纯数字。
如果您阅读了标准,则有相关规定:
两个指向不相关对象的指针(意思是不属于更大对象的一部分,记住结构和数组)不能进行比较,除非相等。
6.5.8 Relational operators
[...]
5 When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript
values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P
points to an element of an array object and the expression Q
points to the last element of the same array object, the pointer expression Q+1
compares greater than P
. In all other cases, the behavior is undefined.
两个指向不相关对象的指针不能相减。
6.5.6 Additive operators
[...]
9 When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the twoarray elements. The size of the result is implementation-defined, and its type (a signed integer type) is ptrdiff_t
defined in the <stddef.h>
header. If the result is not representable in an object of that type, the behavior is undefined. In other words, if the expressions P
and Q
point to, respectively,the i-th and j-th elements of an array object, the expression (P)-(Q)
has the value i−j provided the value fits in an object of type ptrdiff_t
. Moreover, if the expression P
points either to an element of an array object or one past the last element of an array object, and the expression Q
points to the last element of the same array object, the expression ((Q)+1)-(P)
has the same
value as ((Q)-(P))+1
and as -((P)-((Q)+1))
, and has the value zero if the expression P
points one past the last element of the array object, even though the expression (Q)+1
does not point to an element of the array object.91)
可能没有办法将指针表示为数字,因为可能不存在合适的类型。因此,尝试转换可能会导致未定义的行为。
定义行为的任何特定实现并不意味着它不是符合标准的 UB。
6.3.2.3 Pointers
[...]
6 Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of anyinteger type.
7.18.1.4 Integer types capable of holding object pointers
1 The following type designates a signed integer type with the property that any valid pointer to void
can be converted to this type, then converted back to pointer to void
, and the result will compare equal to the original pointer:
intptr_t
The following type designates an unsigned integer type with the property that any valid pointer to void
can be converted to this type, then converted back to pointer to void
, and the result will compare equal to the original pointer:
uintptr_t
These types are optional.
这只是我想不到的,我相信还有更多。
所有引用自 n1256(C99 草案)。
数组一直被要求是连续的。
要回答你的第二个问题,数组元素位于连续的内存位置。这就是为什么要使用指针算法在元素之间移动的原因。
C 标准是否要求指针为(整数)数字?
有人可能会说是的,因为指针算法...
但另一方面,像--
或++
这样的操作可以理解为上一个内存位置、下一个内存位置,取决于它们在标准中的描述方式,实际实现可能使用任何表示来保存指针数据(只要实现了提到的操作)...
想到另一个问题 - C 是否要求 arrays/buffers 等连续,即下一个元素存储在 下一个内存位置(++p
其中 p 是一个指针)?我问是因为您经常可以在网上看到似乎假设它确实如此的实现。
不,指针不必是纯数字。
如果您阅读了标准,则有相关规定:
两个指向不相关对象的指针(意思是不属于更大对象的一部分,记住结构和数组)不能进行比较,除非相等。
6.5.8 Relational operators
[...]
5 When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expressionP
points to an element of an array object and the expressionQ
points to the last element of the same array object, the pointer expressionQ+1
compares greater thanP
. In all other cases, the behavior is undefined.两个指向不相关对象的指针不能相减。
6.5.6 Additive operators
[...]
9 When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the twoarray elements. The size of the result is implementation-defined, and its type (a signed integer type) isptrdiff_t
defined in the<stddef.h>
header. If the result is not representable in an object of that type, the behavior is undefined. In other words, if the expressionsP
andQ
point to, respectively,the i-th and j-th elements of an array object, the expression(P)-(Q)
has the value i−j provided the value fits in an object of typeptrdiff_t
. Moreover, if the expressionP
points either to an element of an array object or one past the last element of an array object, and the expressionQ
points to the last element of the same array object, the expression((Q)+1)-(P)
has the same value as((Q)-(P))+1
and as-((P)-((Q)+1))
, and has the value zero if the expressionP
points one past the last element of the array object, even though the expression(Q)+1
does not point to an element of the array object.91)可能没有办法将指针表示为数字,因为可能不存在合适的类型。因此,尝试转换可能会导致未定义的行为。
定义行为的任何特定实现并不意味着它不是符合标准的 UB。
6.3.2.3 Pointers
[...]
6 Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of anyinteger type.7.18.1.4 Integer types capable of holding object pointers
1 The following type designates a signed integer type with the property that any valid pointer to
void
can be converted to this type, then converted back to pointer tovoid
, and the result will compare equal to the original pointer:intptr_t
The following type designates an unsigned integer type with the property that any valid pointer to
void
can be converted to this type, then converted back to pointer tovoid
, and the result will compare equal to the original pointer:uintptr_t
These types are optional.
这只是我想不到的,我相信还有更多。
所有引用自 n1256(C99 草案)。
数组一直被要求是连续的。
要回答你的第二个问题,数组元素位于连续的内存位置。这就是为什么要使用指针算法在元素之间移动的原因。