与指针比较的元素是否在数组末尾明确定义? [英] Is comparing to a pointer one element past the end of an array well-defined?

查看:95
本文介绍了与指针比较的元素是否在数组末尾明确定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过此问题了解到,增加NULL指针或增加超过数组末尾的做法不是很好-定义的行为:

I learned by this question that incrementing a NULL pointer or incrementing past the end of an array isn't well-defined behavior:

int* pointer = 0;
pointer++;

int a[3];
int* pointer2 = &a[0];
pointer2 += 4; 

但是,如果指向无效位置的指针仅用于比较并且从未访问过该位置的内存该怎么办?

But what If the pointer pointing to a invalid place is only used for comparison and memory at his location is never accessed?

示例:

void exampleFunction(int arrayLen, char** strArray)
{
    for(char** str = strArray; str < strArray + arrayLen; str++) //or even str < &strArray[arrayLen]
    {
        //here *str is always a pointer to the first char of my string
    }
}

在这里,我将指针与指向数组末尾一个元素的指针进行比较.这是行为明确的行为吗?

Here I compare my pointer to a pointer one element past the end of the array. Is this well-defined behavior?

推荐答案

与指针相比,在数组末尾的一步定义得很好.但是,即使您实际上不使用这些指针,您的pointerpointer2示例也是未定义的.

Comparing to a pointer one step past the end of an array is well defined. However, your pointer and pointer2 examples are undefined, even if you do literally nothing with those pointers.

一个指针可能指向数组末尾的一个元素.该指针可能不会被取消引用(否则将是未定义的行为),但可以将其与数组中的另一个指针.

A pointer may point to one element past the end of the array. That pointer may not be dereferenced (otherwise that would be undefined behavior) but is can be compared to another pointer within the array.

C标准的第6.5.6节关于指针添加的内容如下:

Section 6.5.6 of the C standard says the following regarding pointer addition:

8 如果指针操作数和结果都指向同一数组对象的元素,则或在数组的最后一个元素之后 对象,评估不会产生溢出; 否则, 行为未定义.如果结果指向最后一个元素 数组对象的值,不得用作一元*的操作数 被评估的运算符.

8 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.5.8节中有关指针比较的内容如下:

Section 6.5.8 says the following regarding pointer comparisons:

5 比较两个指针时,结果取决于所指向对象在地址空间中的相对位置. 如果是两个 指向对象类型的指针都指向同一个对象,或者都指向 在同一数组对象的最后一个元素之后,它们进行比较 相等.如果指向的对象是同一聚合的成员 对象,指向稍后声明的结构成员的指针比较大 而不是指向结构中早先声明的成员的指针,以及 下标值较大的数组元素的指针进行比较 大于指向同一数组的元素的指针,具有较低的指针 下标值.指向同一联合对象的成员的所有指针 比较相等. 如果表达式P指向数组的元素 对象和表达式Q指向同一对象的最后一个元素 数组对象,指针表达式Q + 1比较大于P. 在所有其他情况下,行为是不确定的.

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

pointer1的情况下,它开始指向NULL.递增此指针会引起未定义的行为,因为它没有指向有效的对象.

In the case of pointer1, it starts out pointing to NULL. Incrementing this pointer invokes undefined behavior because it don't point to a valid object.

对于pointer2,它增加4,将它放置在数组末尾的两个元素中,而不是一个,因此这也是未定义的行为.如果将其增加3,则行为将得到很好的定义.

For pointer2, it is increased by 4, putting it two elements past the end of the array, not one, so this is again undefined behavior. Had it been increased by 3, the behavior would be well defined.

这篇关于与指针比较的元素是否在数组末尾明确定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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