我可以把一个阵列的一过去,中高端元素的地址? [英] May I take the address of the one-past-the-end element of an array?

查看:119
本文介绍了我可以把一个阵列的一过去,中高端元素的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/988158/take-the-address-of-a-one-past-the-end-array-element-via-subscript-legal-by-the\">Take通过下标一过去,中端阵列元素的地址:由C ++标准合法与否


  int数组[10];为int * A =阵列+ 10; //定义良好的
INT * B =放大器;阵列[10]; // 不确定...

时的最后一行有效还是无效?


解决方案

是的,你可以把地址一举超越数组的结束,但你不能取消对它的引用。为了您的10个项目的数组,阵列+ 10 会工作。它已经争论了几次(由委员会,等等)是否&放大器;阵列[10] 真的会导致未定义的行为或没有(如果这样做,是否真的应该)。与它的底线是,至少按照目前的标准(C和C ++),它正式将导致未定义的行为,但如果有一个编译器,它实际上不工作,没有人在任何参数已经能够找到或引用它。

编辑:这一次我的记忆是正确的一半 - 这是(部分)的正式的缺陷报告该委员会,并至少有一些委员会成员(例如,汤姆·梅)认为措辞已经改变,所以它的的导致未定义行为。 OTOH,在DR从2000年的日期,状态仍然是起草了,所以这是值得商榷是否真的固定的,或曾经可能是(我没有通过N3090 / 3092看出来)。

在C99,但是,它显然的的未定义的行为。

Possible Duplicate:
Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not?

int array[10];

int* a = array + 10;   // well-defined
int* b = &array[10];   // not sure...

Is the last line valid or not?

解决方案

Yes, you can take the address one beyond the end of an array, but you can't dereference it. For your array of 10 items, array+10 would work. It's been argued a few times (by the committee, among others) whether &array[10] really causes undefined behavior or not (and if it does, whether it really should). The bottom line with it is that at least according to the current standards (both C and C++) it officially causes undefined behavior, but if there's a single compiler for which it actually doesn't work, nobody in any of the arguments has been able to find or cite it.

Edit: For once my memory was half correct -- this was (part of) an official Defect Report to the committee, and at least some committee members (e.g., Tom Plum) thought the wording had been changed so it would not cause undefined behavior. OTOH, the DR dates from 2000, and the status is still "Drafting", so it's open to question whether it's really fixed, or ever likely to be (I haven't looked through N3090/3092 to figure out).

In C99, however, it's clearly not undefined behavior.

这篇关于我可以把一个阵列的一过去,中高端元素的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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