Multidimensional arrays

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

    Multidimensional arrays

    What's the best way of dynamically allocating a multidim array from the
    following:

    int **a = new int*[size];
    a[0] = new int[size];
    .... and so on

    or:

    int (*a)[10] = new int[10][10];


    by the way, how do we pass arrays delcared by the second way to
    functions? What really is the difference between (*a)[] and *a[]..
    actually i kind of understand the difference but it doesn't hit me that
    well.. I can't interpret what (*a)[] really means. Thanks!

    James

  • Buster

    #2
    Re: Multidimensiona l arrays


    "Buster" <noone@nowhere. com> wrote in message[color=blue]
    > "James" <snotty@jamison .snort> wrote in message[color=green]
    > > What's the best way of dynamically allocating a multidim array from the
    > > following:
    > >
    > > int **a = new int*[size];
    > > a[0] = new int[size];
    > > ... and so on[/color]
    >
    > Use this way if the last dimension is not a compile-time constant.[/color]

    I should have said 'any dimension except the first'.

    [color=blue]
    > You can also have 'rows' (say) with different numbers of 'columns'
    > using this idiom.[color=green]
    > > or:
    > >
    > > int (*a)[10] = new int[10][10];[/color]
    >
    > This way can be simpler but only works if the last dimension is a
    > compile-time constant.[/color]

    And here, 'every dimension except the first'.


    Regards,
    Buster.


    Comment

    • Allan Bruce

      #3
      Re: Multidimensiona l arrays


      "James" <snotty@jamison .snort> wrote in message
      news:3f422e5e$1 @clarion.carno. net.au...[color=blue]
      > What's the best way of dynamically allocating a multidim array from the
      > following:
      >
      > int **a = new int*[size];
      > a[0] = new int[size];
      > ... and so on
      >
      > or:
      >
      > int (*a)[10] = new int[10][10];
      >
      >
      > by the way, how do we pass arrays delcared by the second way to
      > functions? What really is the difference between (*a)[] and *a[]..
      > actually i kind of understand the difference but it doesn't hit me that
      > well.. I can't interpret what (*a)[] really means. Thanks!
      >
      > James
      >[/color]

      I use a pointer to an array of pointers to an array of <type>, like this

      int **array;

      array = new int*[width];
      for (int loop=0; loop<width; ++loop)
      array[loop] = new int[height];

      // do some things with it like
      // array[3][4] = rand(); // or whatever

      for (loop=0; loop<width; ++loop)
      delete []array[loop];
      delete [] array;

      Allan


      Comment

      • Mike Wahler

        #4
        Re: [FAQ] Multidimensiona l arrays


        James <snotty@jamison .snort> wrote in message
        news:3f422e5e$1 @clarion.carno. net.au...[color=blue]
        > What's the best way of dynamically allocating a multidim array from the
        > following:
        >
        > int **a = new int*[size];
        > a[0] = new int[size];
        > ... and so on
        >
        > or:
        >
        > int (*a)[10] = new int[10][10];[/color]




        -Mike



        Comment

        • Axter

          #5
          Re: Multidimensiona l arrays


          Here's a class that creates a 2 dimensional array very easily and
          efficiently:



          template < class T, int ROW_T = 0, int COL_T = 0 >

          class dynamic_2d_arra y

          {

          public:

          dynamic_2d_arra y(int row, int col):m_row(row) ,m_col(col),
          m_data((row!=0& &col!=0)?new T[row*col]:NULL){}

          dynamic_2d_arra y():m_row(ROW_T ),m_col(COL_T), m_data(new
          T[ROW_T*COL_T])

          {if (!COL_T || !ROW_T) {int x[ROW_T] = {{ROW_T}};int y[COL_T] =
          {{x[0]}};}}

          ~dynamic_2d_arr ay(){if(m_data) delete []m_data;}

          T* operator[](int i) {return (m_data + (m_col*i));}

          T const*const operator[](int i) const {return (m_data + (m_col*i));}

          private:

          const int m_row;

          const int m_col;

          T* m_data;

          };



          The above class can be delcare and used in the following maner:

          dynamic_2d_arra y < char > My_dynamic_2d_a rray(3, 20);

          My_dynamic_2d_a rray[2][11] = 99;

          cout << My_dynamic_2d_a rray[2][11] << endl;



          You can also use a vector<vector<c har> > type object.

          For more information on this class, see the following link:


          c_Title=How+to+ create+dynamic+ two%2Ddimension al+arrays&Forum _Title=-
          C%2FC%2B%2B



          Also check out the multidimensiona l class in the following link:


          _Title=How+to+c reate+dynamic+M ulti%2Ddimensio nal+arrays&Foru m_Title=-
          C%2FC%2B%2B


          --
          Top ten Expert at Experts-Exchange


          Posted via http://dbforums.com

          Comment

          Working...