Is accessing an element of a multidimensional array out of bounds undefined behavior?

纵然是瞬间 提交于 2021-01-27 04:13:10

问题


Pardon the confusing question title, but I was unsure how to phrase it more clearly.

In C, accessing an array out of bounds is classified as undefined behavior. However, array elements are guaranteed to be laid out contiguously in memory, and the array subscript operator is syntactic sugar for pointer arithmetic (e.g x[3] == *(x + 3)). Therefore, I would personally expect the behavior of the code below to be well-defined:

int array[10][10];
int i = array[0][15]; // i == array[1][5]?

If my interpretation of the standard is correct, this would be undefined behavior. Am I wrong?


回答1:


According to the standard, it is clearly undefined behaviour as such a case is explicitly listed in the section J.2 undefined behaviour (found in an online C99 standard draft):

An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5]) (6.5.6).

It can still be the case that your example will work, and actually I have seen a lot of such cases in C code; However, to be accurate, it is UB.




回答2:


The Standard makes very clear that given unsigned char arr[10][10];, an attempt to access arr[0][x] would yield UB if x exceeds 9.

I think it is equally clear, however, that the authors of the Standard intended to allow code to take the address of any object, including a multi-dimensional array, as a character pointer, and then index that pointer to access all the bytes of the object.

If the Standard were to say that the arr[0] yields a pointer of type char* which can only be used to access the first ten elements, but (char*)arr would yield a pointer that can access the entire array, that would accommodate both objectives above, but I see nothing in the Standard that would suggest that arr[0] and (char*)arr are not equivalent to each other.

Most likely, the authors of the Standard expected that implementations would seek to behave sensibly in such corner cases whether or not the Standard described them fully. I'm not sure whether clang and gcc conform to such expectations with regard to this particular issue, but such expectations don't hold true in general.



来源:https://stackoverflow.com/questions/62249658/is-accessing-an-element-of-a-multidimensional-array-out-of-bounds-undefined-beha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!