Implementation of 2-dimensional arrays

Discussion in 'C Programming' started by Ioannis Vranos, Apr 9, 2011.

  1. 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.
    Ioannis Vranos, Apr 9, 2011
    #1
    1. Advertising

  2. Ioannis Vranos

    Stefan Ram Guest

    Ioannis Vranos <> writes:
    >arrays


    »arrays are stored in row-major order«

    ISO/IEC 9899:1999 (E), 6.5.2.1#3
    Stefan Ram, Apr 9, 2011
    #2
    1. Advertising

  3. Ioannis Vranos

    jacob navia Guest

    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
    jacob navia, Apr 9, 2011
    #3
  4. Ioannis Vranos

    James Kuyper Guest

    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
    James Kuyper, Apr 9, 2011
    #4
  5. Ioannis Vranos

    Eric Sosman Guest

    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
    Eric Sosman, Apr 9, 2011
    #5
  6. Ioannis Vranos

    James Kuyper Guest

    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
    James Kuyper, Apr 9, 2011
    #6
  7. 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.
    Ioannis Vranos, Apr 10, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    423
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,914
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    641
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    963
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    195
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page