Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Implementation of 2-dimensional arrays

Reply
Thread Tools

Implementation of 2-dimensional arrays

 
 
Ioannis Vranos
Guest
Posts: n/a
 
      04-09-2011
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.
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      04-09-2011
Ioannis Vranos <> writes:
>arrays


»arrays are stored in row-major order«

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

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      04-09-2011
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

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      04-09-2011
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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-09-2011
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
d
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      04-09-2011
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
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      04-10-2011
Stefan Ram wrote:

> Ioannis Vranos <> writes:
>>arrays

>
> »arrays are stored in row-major order«
>
> ISO/IEC 9899:1999 (E), 6.5.2.1#3



Thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insertion Sort : C++ implementation 100 times slower than C implementation sanket C++ 7 11-03-2011 05:00 AM
The behavior of "equals" method of Arrays.asList() implementation Alex J Java 4 07-22-2011 12:36 AM
Knowing the implementation, are all undefined behaviours become implementation-defined behaviours? Michael Tsang C Programming 54 03-30-2010 07:46 AM
Knowing the implementation, are all undefined behaviours become implementation-defined behaviours? Michael Tsang C++ 32 03-01-2010 09:15 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57