[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Implementation of 2-dimensional arrays

Ioannis Vranos

4/9/2011 4:06:00 PM

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.
6 Answers

ram

4/9/2011 4:16:00 PM

0

Ioannis Vranos <no@spam.gr> writes:
>arrays

»arrays are stored in row-major order«

ISO/IEC 9899:1999 (E), 6.5.2.1#3

jacob navia

4/9/2011 4:19:00 PM

0

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

James Kuyper

4/9/2011 4:34:00 PM

0

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

Eric Sosman

4/9/2011 7:29:00 PM

0

On 4/9/2011 12:34 PM, James Kuyper wrote:
> 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.

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

--
Eric Sosman
esosman@ieee-dot-org.invalid

James Kuyper

4/9/2011 8:28:00 PM

0

On 04/09/2011 03:28 PM, Eric Sosman wrote:
> On 4/9/2011 12:34 PM, James Kuyper wrote:
>> 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.
>
> 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.


--
James Kuyper

Ioannis Vranos

4/10/2011 12:46:00 PM

0

Stefan Ram wrote:

> Ioannis Vranos <no@spam.gr> writes:
>>arrays
>
> »arrays are stored in row-major order«
>
> ISO/IEC 9899:1999 (E), 6.5.2.1#3


Thanks.