James Kuyper
4/9/2011 4:34:00 PM
On 04/09/2011 12:05 PM, Ioannis Vranos wrote:
> Hi all,
>
> Is it guaranteed that ==> all C implementations, implement two-dimensional�
> arrays in the style:
>
>
> Logical:
>
> A11 A12 A13
> A21 A22 A23
> A31 A32 A33
>
>
> Physical:
>
> A11 A21 A31 A12 A22 A32 A13 A23 A33
>
>
> that is, column after column (and not line after line)?
Assuming
int a[3][3]
and assuming that by A12 you are referring to a[0][1], then yes, this is
guaranteed.
People often assume, as a result, that it would be perfectly permissible
to write:
int *pa = a[0];
pa[4] = 5;/* now a[0][2] == 5 */
or, more directly:
a[0][4] = 6;
However, there is an interpretation of the standard (which is heavily
disputed, though I consider it correct) which says that the behavior of
pa[4] (or equivalently, a[0][4]) is undefined. If anything did go wrong
with such code, it would not be because the elements of 'a' are stored
discontinuously. It could happen because the implementation performs
run-time bounds checking on the pointer created by the expression a[0].
However, a more likely possibility is that it might fail because of a
subtle optimization that was based upon the correct assumption that
a[0][n] could never alias a[1][m] for any permissible values of n and m.
Such an optimization might produce unexpected (but perfectly conforming)
behavior if impermissible values of m or n are used.
--
James Kuyper