指针到阵列的重叠阵列结束 [英] Pointer-to-array overlapping end of array

查看:129
本文介绍了指针到阵列的重叠阵列结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code正确吗?

int arr[2];

int (*ptr)[2] = (int (*)[2]) &arr[1];

ptr[0][0] = 0;

显然 PTR [0] [1] 将通过访问了的界限改编无效。

注意:毫无疑问, PTR [0] [0] 指定相同的内存位置为改编[ 1] ;问题是,我们是否允许通过 PTR 来访问内存位置。 这里是当前pression并指定相同的内存的一些例子的位置,但它不许可访问的存储器位置的方式。

Note: There's no doubt that ptr[0][0] designates the same memory location as arr[1]; the question is whether we are allowed to access that memory location via ptr. Here are some more examples of when an expression does designate the same memory location but it is not permitted to access the memory location that way.

注意2:同时考虑 ** PTR = 0; 。正如马克货车Leeuwen指出, PTR [0] 等同于 *(PTR + 0),但是 PTR + 0 似乎陷入指针运算部的犯规。但是,通过使用 * PTR 来代替,这是可以避免的。

Note 2: Also consider **ptr = 0; . As pointed out by Marc van Leeuwen, ptr[0] is equivalent to *(ptr + 0), however ptr + 0 seems to fall foul of the pointer arithmetic section. But by using *ptr instead, that is avoided.

推荐答案

不是一个答案,但评论,我似乎字不能很好地而不文本墙:

Not an answer but a comment that I can't seem to word well without being a wall of text:

由于阵列保证存储的内容连续,使他们能够遍历使用指针。如果我可以采取一个指针开始的数组,并陆续增加该指针,直到我已访问数组中的每个元素都那么肯定,使该阵列可以为一系列任何类型它是由作者进行访问的声明

Given arrays are guaranteed to store their contents contiguously so that they can be 'iterated over' using a pointer. If I can take a pointer to the begin of an array and successively increment that pointer until I have accessed every element of the array then surely that makes a statement that the array can be accessed as a series of whatever type it is composed of.

当然的组合:
1)数组[X]地址为数组存储其第一要素
2)一个指针到它的逐次增量足以访问下一个项
3)数组[X-1]遵循相同的规则

Surely the combination of: 1) Array[x] stores its first element at address 'array' 2) Successive increments of the a pointer to it are sufficient to access the next item 3) Array[x-1] obeys the same rules

那么它应该是合法的,至少看地址'阵',好像它是类型的数组[X-1],而不是类型的数组[X]

Then it should be legal to at least look at the address 'array' as if it were type array[x-1] instead of type array[x].

此外给出的点关于是连续的,并且如何指针数组中的元素都表现,肯定它必须是合法的,然后组阵列的任何连续子集[X]如数组[Y],其中y&下; x和它的上限不超过阵列的程度[X]

Furthermore given the points about being contiguous and how pointers to elements in the array have to behave, surely it must be legal to then group any contiguous subset of array[x] as array[y] where y < x and it's upper bound does not exceed the extent of array[x].

由于不是一个语言律师这只是我喷一些垃圾。我在这个讨论中,虽然结果很感兴趣。

Not being a language-lawyer this is just me spouting some rubbish. I am very interested in the outcome of this discussion though.

编辑:

在进一步考虑原来的code的,在我看来,数组本身是非常在许多方面的特殊情况。他们衰减到一个指针,而我的认为的可别名按我刚才在前面这篇文章说。

On further consideration of the original code, it seems to me that arrays are themselves very much a special case in many regards. They decay to a pointer, and I believe can be aliased as per what I just said earlier in this post.

因此​​,没有任何standardese备份我的拙见,数组不能真正无效或未定义作为一个整体,如果它并没有真正得到的待遇作为一个整体统一。

So without any standardese to back up my humble opinion, an array can't really be invalid or 'undefined' as a whole if it doesn't really get treated as a whole uniformly.

什么是获得均匀的处理是单个元素。因此,我认为它才有意义谈谈访问特定元素是否有效或定义。

What does get treated uniformly are the individual elements. So I think it only makes sense to talk about whether accessing a specific element is valid or defined.

这篇关于指针到阵列的重叠阵列结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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