单字节过指针用C是否仍然有效? [英] One-byte-off pointer still valid in C?

查看:125
本文介绍了单字节过指针用C是否仍然有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能是错的,但我似乎记得,对于一个给定的内存分配,例如。

I might be mistaken, but I seem to remember that for a given memory allocation, e.g.

char *p = malloc(4);

指针 P 是所有字节分配的的为超出分配的第一个字节中的一个有效的指针。

the pointer p is a valid pointer for all bytes within the allocation and for the first byte beyond that allocation.

因此​​,通过指针访问内存 P 仅偏移 P [0] .. P [3] 是有效的。但对于指针比较&放大器;(P [4])也将是一个有效的指针

Thus, to access memory through the pointer p only offsets p[0] .. p[3] are valid. But for pointer comparison &( p[4] ) would also be be a valid pointer.

这是否正确,并在C标准(链接)它这么说?似乎 6.5.9 P6 可能暗示到正确的对于答案的方向,但它是一个有点模糊仍。

Is that correct, and where in the C Standard (link) does it say so? It seems that 6.5.9 p6 might hint into the right direction for the answer, but it's a bit fuzzy still.

推荐答案

这答案假定 P 的char *

但对于指针比较和;(第[4])将被还是有效

but for pointer comparison &( p[4] ) would be also be valid.

指针 P + 4 (或及(P [4])有效期为比较点+ N N 是{0,1,2,3,4}与 < < = == 这是在C11 6.5注明。 0.8:5:

The pointer p + 4 (or &( p[4] ) is valid for comparison to p + N when N is in {0, 1, 2, 3, 4} with <, <=, or ==. This is noted in C11 6.5.8:5:

当两个指针进行比较,其结果取决于物体的地址空间中的相对位置所指向。如果两个指针对象类型都指向同一个对象,或两者一点一点过去相同的数组对象的最后一个元素,它们的比较平等的。如果对象指向是同一个聚合对象的成员,指针结构体成员宣布后比较不是指向先前在结构中声明成员更大,指针具有较大的下标值的数组元素比较不是指针更大,以相同的数组中的元素低标
  值。所有指向同一个联盟对象的成员比较平等的。如果EX pression p指向数组对象的元素和前pression Q指向相同的数组对象的最后一个元素,指针前pression Q + 1大于P.更大的比较在其他情况下,该行为是不确定的。

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 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.

然而, P + 4 无效与比较== ,比方说,&放大器; X ,其中 X 是另一个变量。这是(对我最大的C-标准解码)未指定的行为。 (当然无点的+ N 有效期为与&LT比较= &安培; X

However, p+4 is not valid for comparison with == to, say, &X where X is another variable. This is (to the best of my C-standard deciphering) unspecified behavior. (And of course none of p + N is valid for comparison with <= to &X.)

两个指针比较相等当且仅当两者都为空指针,​​两者都指向同一对象(包括一个指针指向一个物体,并在其开头的子对象)或功能,二者是指向一个过去的最后一个元素相同的数组对象,一个是指向一点一数组对象的结束,另一个是一个指向恰好紧跟在地址space.109第一个数组对象不同的数组对象的开始)

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.109)

109),因为它们是一个结构的,它们之间没有填充一个较大的阵列或相邻构件的相邻的元件的两个物体可以是在存储器中相邻,或因为实现选择将它们如此,即使它们是不相关的。如果事先无效的指针操作(比如访问外线数组边界)产生不确定的行为,后续的比较还会产生不确定的行为。

109) Two objects may be adjacent in memory because they are adjacent elements of a larger array or adjacent members of a structure with no padding between them, or because the implementation chose to place them so, even though they are unrelated. If prior invalid pointer operations (such as accesses outside array bounds) produced undefined behavior, subsequent comparisons also produce undefined behavior.

(C11 6.5.9:6)

(C11 6.5.9:6)

严格地说,标准似乎并没有任何地方说 P + 4 == NULL 被定义成(修改为RICI尖出,对于只领取津贴 p + 4 等于是,如果是,由于NULL不是任何对象的地址,恰好紧跟......不同的数组对象的开始,它遵循 p + 4 == NULL 是假的)。

Strictly speaking, the standard does not seem to say anywhere that p + 4 == NULL is defined either ( as rici pointed out, the only allowance for p + 4 to be equal to q is if q is "the start of a different array object that happens to immediately follow…". Since NULL is not the address of any object, it follows that p + 4 == NULL is false).

博客后看着这个和其他指针比较用C

This blog post looks at this and other pointer comparisons in C.

这篇关于单字节过指针用C是否仍然有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆