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

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.


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.