Are 2D arrays contiguous?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tristan Miller

    Are 2D arrays contiguous?

    Greetings.

    Are statically allocated two-dimensional arrays guaranteed to be
    contiguous, or is it possible the compiler will allocate padding after
    each row? Is the following code OK?

    #include <string.h>

    int main(void) {
    char a[5][5];
    memset(a, 'x', 25);
    return 0;
    }

    Regards,
    Tristan

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= < In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
  • daniel

    #2
    Re: Are 2D arrays contiguous?

    On Aug 15, 1:38 pm, Tristan Miller <psychon...@not hingisreal.com>
    wrote:
    Greetings.
    >
    Are statically allocated two-dimensional arrays guaranteed to be
    contiguous, or is it possible the compiler will allocate padding after
    each row?  Is the following code OK?
    >
    #include <string.h>
    >
    int main(void) {
      char a[5][5];
      memset(a, 'x', 25);
      return 0;
    >
    }
    >
    Regards,
    Tristan
    >
    --
       _
      _V.-o  Tristan Miller [en,(fr,de,ia)]  ><  Space is limited
     / |`-'  -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= < In a haiku, so it's hard
    (7_\\    http://www.nothingisreal.com/  ><  To finish what you

    As far as I know they are.

    Comment

    • pete

      #3
      Re: Are 2D arrays contiguous?

      Tristan Miller wrote:
      Greetings.
      >
      Are statically allocated two-dimensional arrays guaranteed to be
      contiguous,
      Yes.
      or is it possible the compiler will allocate padding after
      each row?
      No.
      Is the following code OK?
      Yes.
      #include <string.h>
      >
      int main(void) {
      char a[5][5];
      memset(a, 'x', 25);
      return 0;
      }
      --
      pete

      Comment

      • Keith Thompson

        #4
        Re: Are 2D arrays contiguous?

        Tristan Miller <psychonaut@not hingisreal.comw rites:
        Are statically allocated two-dimensional arrays guaranteed to be
        contiguous, or is it possible the compiler will allocate padding after
        each row? Is the following code OK?
        >
        #include <string.h>
        >
        int main(void) {
        char a[5][5];
        memset(a, 'x', 25);
        return 0;
        }
        Yes, because you're treating the object as an array of characters.

        On the other hand, this:

        a[0][5] = 'y';

        will probably set a[1][0] to 'y', but it's not guaranteed; strictly
        speaking, it's undefined behavior.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        Working...