在c和数组边界指针运算 [英] Pointer arithmetic in c and array bounds

查看:189
本文介绍了在c和数组边界指针运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过其中有一些C常见问题解答,我发现了这个声明一网页浏览

I was browsing through a webpage which had some c FAQ's, I found this statement made.

类似地,如果有10个元素和ip
  指向[3],您不能计算或
  访问IP + 10或IP - 5
(有
  一种特殊情况:可以,在这
  情况下,计算,但不连接,一
  指针不存在的元素
  刚好超出数组的末尾,
  在这种情况下是&放大器;一个[10]

Similarly, if a has 10 elements and ip points to a[3], you can't compute or access ip + 10 or ip - 5. (There is one special case: you can, in this case, compute, but not access, a pointer to the nonexistent element just beyond the end of the array, which in this case is &a[10].

我被弄得声明

您不能计算IP + 10

you can't compute ip + 10

我能理解访问元素出界是不确定的,但计算!!!

I can understand accessing the element out of bounds is undefined, but computing!!!.

我写了下面的代码片段其中的计算的(让我知道这是个什么网站意思的电脑的)指针超出范围。

I wrote the following snippet which computes (let me know if this is what the website meant by computing) a pointer out-of-bounds.

#include <stdio.h>                                                                                                                                                                  

int main()                                                                                                                                                                          
{                                                                                                                                                                                   
        int a[10], i;                                                                                                                                                               
        int *p;                                                                                                                                                                     

        for (i = 0; i<10; i++)                                                                                                                                                      
                a[i] = i;                                                                                                                                                           

        p = &a[3];                                                                                                                                                                  

        printf("p = %p and p+10 = %p\n", p, p+10);                                                                                                                                  
        return 0;                                                                                                                                                                   
}                     

$ ./a.out                                                                                                                                     
p = 0xbfa53bbc and p+10 = 0xbfa53be4     

我们可以看到,P + 10指向10个元素(40字节)过去页。那么究竟是什么在网页中所作的发言的意思。难道我错间$ P $磅的东西。

We can see that p + 10 is pointing to 10 elements(40 bytes) past p. So what exactly does the statement made in the webpage mean. Did I mis-interpret something.

即使在K&放大器; R(A.7.7)这句话是由:

Even in K&R (A.7.7) this statement is made:

+操作的结果是
  操作数的总和。一个指向
  对象在阵列和任何一个值
  整体式可加入。 ...的
  总和是相同类型的指针
  原始指针,指向
  相同的阵列中的另一个目的,
  从原来的适当偏移
  目的。因此,如果P是一个指向
  对象数组,前pression P + 1
  是指向在下一个对象
  数组。的如果总和指针指向
  数组的边界之外,
  除了在第一位置以外
  高端,其结果是
  不确定的。

什么是不确定的意思。这是否意味着总和将是不确定的,或者当我们引用这些行为是不确定它只是意思。当我们不引用这些操作,甚至不定,才算指针超出边界的元素。

What does being "undefined" mean. Does this mean the sum will be undefined, or does it only mean when we dereference it the behavior is undefined. Is the operation undefined even when we do not dereference it and just calculate the pointer to element out-of-bounds.

推荐答案

未定义行为意味着正是:的绝对什么事情都可能发生的。它可以静静地成功,也可能失败默默,可能你的程序崩溃,它可能蓝屏您的操作系统,也可以删除你的硬盘。其中有些是不太可能的,但个个都是允许的行为的至于C语言标准是关于的。

Undefined behavior means exactly that: absolutely anything could happen. It could succeed silently, it could fail silently, it could crash your program, it could blue screen your OS, or it could erase your hard drive. Some of these are not very likely, but all of them are permissible behaviors as far as the C language standard is concerned.

在这种特殊情况下,是的,C标准是说,即使的计算有效数组边界以外的指针的地址的,没有取消引用它,是不确定的行为。它说,这样做的原因是,在有些情况下做这样的计算可能会导致可能会导致某种形式的故障一些神秘的系统。例如,您可能在寻址存储​​器的最后一个阵列,构建一个指针超出了会导致产生一个陷阱或故障特殊的地址寄存器溢出。 C标准要允许以这种行为为尽可能便携。

In this particular case, yes, the C standard is saying that even computing the address of a pointer outside of valid array bounds, without dereferencing it, is undefined behavior. The reason it says this is that there are some arcane systems where doing such a calculation could result could result in a fault of some sort. For example, you might have an array at the very end of addressable memory, and constructing a pointer beyond that would cause an overflow in a special address register which generates a trap or fault. The C standard wants to permit this behavior in order to be as portable as possible.

在现实中,虽然,你会发现,如果没有取消引用它构建这样一个无效的地址对绝大多数系统的良好定义的行为,你会普遍使用遇到。除非你试图取消对它的引用创建一个无效的内存地址不会有任何不良影响。但当然,这是更好地避免创建这些无效的地址,以便您的code会很好地工作,甚至在那些神秘的系统。

In reality, though, you'll find that constructing such an invalid address without dereferencing it has well-defined behavior on the vast majority of systems you'll come across in common usage. Creating an invalid memory address will have no ill effects unless you attempt to dereference it. But of course, it's better to avoid creating those invalid addresses so that your code will work perfectly even on those arcane systems.

这篇关于在c和数组边界指针运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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