Implementation of 2-dimensional arrays

I

Ioannis Vranos

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)?



Thanks.
 
J

jacob navia

Le 09/04/11 18:05, Ioannis Vranos a écrit :
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)?



Thanks.

No, you are wrong.
The layout is

A11 A12 A13 A21 A22 A23 A31 A32 A33

And do not forget that indices start at zero... It should
be:

A00 A01 ... etc
 
J

James Kuyper

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.
 
E

Eric Sosman

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.

ITYM "guaranteed false." (Either that, or we're both having
trouble with the O.P.'s notation.) Putting it purely in C terms:

int a[3][3];
assert (&a[0][1] == &a[0][0] + 1);
assert (&a[0][2] == &a[0][0] + 2);
assert (&a[1][0] == &a[0][0] + 3);
...
assert (&a[2][2] == &a[0][0] + 9);

/* Also, noting that the "stride" is different: */
assert (&a[1] == &a[0] + 1);
assert (&a[2] == &a[0] + 2);
 
J

James Kuyper

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.

ITYM "guaranteed false." (Either that, or we're both having
trouble with the O.P.'s notation.)

You're right; I had the trouble, not you. He was clear enough - I just
didn't pay close enough attention to what he was saying.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top