Given
int blah(int x, int y)
{
int a[2][5];
a[1][0] = x;
a[0][y] = 9;
return a[1][0];
}
nothing in the Standard would forbid a compiler from recoding that as int blah(int x, int y) { return x; }
, nor trapping (or doing anything whatsoever) when y>=5
, since a[0]
and a[1]
are distinct arrays of five elements each. In cases where the last element of an indirectly-accessed structure is a single-element array, compilers have generally included code to allow pointer arithmetic on that array to yield a pointer to storage outside the structure. While such pointer arithmetic would be forbidden by the Standard, it enables useful constructs which could not be practically implemented in any standard-compliant fashion prior to C99.
Note that adding 5 to a[0]
would yield an int*
that compares identical to a[1]
, but the fact that a "one-past" pointer compares equal to a pointer which identifes the next object in memory does not imply that it may be safely used to access the latter object. Such accesses may often work, but that doesn't mean compilers are required to have them do so.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…