指向数组第一个元素之前的指针 [英] Pointer to one before first element of array

查看:91
本文介绍了指向数组第一个元素之前的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中,当指针指向同一数组或数组末尾的一个元素时,算术和比较的定义就很好.那么在数组第一个元素之前的那个呢?只要不取消引用就可以吗?

It is said in C that when pointers refer to the same array or one element past the end of that array the arithmetics and comparisons are well defined. Then what about one before the first element of the array? Is it okay so long as I do not dereference it?

给予

int a[10], *p;
p = a;

(1)编写-p 是否合法?

(2)在表达式中写入 p-1 是否合法?

(2) Is it legal to write p-1 in an expression?

(3)如果(2)可以,我可以断言 p-1<一个?

(3) If (2) is okay, can I assert that p-1 < a?

对此有一些实际问题.考虑一个 reverse()函数,该函数可以反转以'\ 0'结尾的C字符串.

There is some practical concern for this. Consider a reverse() function that reverses a C-string that ends with '\0'.

#include <stdio.h>

void reverse(char *p)
{
    char *b, t;

    b = p;
    while (*p != '\0')
        p++;
    if (p == b)      /* Do I really need */
        return;      /* these two lines? */
    for (p--; b < p; b++, p--)
        t = *b, *b = *p, *p = t;
}

int main(void)
{
    char a[] = "Hello";

    reverse(a);
    printf("%s\n", a);
    return 0;
}

我真的需要在代码中进行检查吗?

Do I really need to do the check in the code?

请从语言律师/实践的角度分享您的想法,以及如何应对这种情况.

Please share your ideas from language-lawyer/practical perspectives, and how you would cope with such situations.

推荐答案

(1)编写--p是否合法?

(1) Is it legal to write --p?

它是C语法中允许的合法"名称,但它会调用未定义的行为.为了找到标准中的相关部分,-p 等效于 p = p-1 (仅评估 p 一次).然后:

It's "legal" as in the C syntax allows it, but it invokes undefined behavior. For the purpose of finding the relevant section in the standard, --p is equivalent to p = p - 1 (except p is only evaluated once). Then:

C17 6.5.6/8

C17 6.5.6/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.

评估调用未定义的行为,这意味着是否取消引用指针都没有关系-您已经调用了未定义的行为.

The evaluation invokes undefined behavior, meaning it doesn't matter if you de-reference the pointer or not - you already invoked undefined behavior.

此外:

C17 6.5.6/9:

C17 6.5.6/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;

如果您的代码违反了ISO标准中的必须",则会调用未定义的行为.

If your code violates a "shall" in the ISO standard, it invokes undefined behavior.

(2)在表达式中写p-1是否合法?

(2) Is it legal to write p-1 in an expression?

与(1)相同,未定义行为.

Same as (1), undefined behavior.

作为在实践中如何引起问题的示例:假设将数组放置在有效内存页的最开始.当您在该页面之外递减时,可能会有硬件异常或指针陷阱表示.对于微控制器来说,这并不是完全不可能的情况,特别是当它们使用分段存储器映射时.

As for examples of how this could cause problems in practice: imagine that the array is placed at the very beginning of a valid memory page. When you decrement outside that page, there could be a hardware exception or a pointer trap representation. This isn't a completely unlikely scenario for microcontrollers, particularly when they are using segmented memory maps.

这篇关于指向数组第一个元素之前的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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