Multidimensional arrays

Discussion in 'C++' started by James, Aug 19, 2003.

  1. James

    James Guest

    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
    James, Aug 19, 2003
    #1
    1. Advertising

  2. James

    Buster Guest

    "Buster" <> wrote in message
    > "James" <> wrote in message
    > > 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

    >
    > Use this way if the last dimension is not a compile-time constant.


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


    > You can also have 'rows' (say) with different numbers of 'columns'
    > using this idiom.
    > > or:
    > >
    > > int (*a)[10] = new int[10][10];

    >
    > This way can be simpler but only works if the last dimension is a
    > compile-time constant.


    And here, 'every dimension except the first'.


    Regards,
    Buster.
    Buster, Aug 19, 2003
    #2
    1. Advertising

  3. James

    Allan Bruce Guest

    "James" <> wrote in message
    news:3f422e5e$...
    > 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
    >


    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
    Allan Bruce, Aug 19, 2003
    #3
  4. James

    Mike Wahler Guest

    Mike Wahler, Aug 19, 2003
    #4
  5. James

    Axter Guest

    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_array

    {

    public:

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

    dynamic_2d_array():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_array(){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_array < char > My_dynamic_2d_array(3, 20);

    My_dynamic_2d_array[2][11] = 99;

    cout << My_dynamic_2d_array[2][11] << endl;



    You can also use a vector<vector<char> > type object.

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

    http://axter.com/faq/topic.asp?TOPIC_ID=60&FORUM_ID=4&CAT_ID=9&Topi-
    c_Title=How+to+create+dynamic+two%2Ddimensional+arrays&Forum_Title=-
    C%2FC%2B%2B



    Also check out the multidimensional class in the following link:

    http://axter.com/faq/topic.asp?TOPIC_ID=61&FORUM_ID=4&CAT_ID=9&Topic-
    _Title=How+to+create+dynamic+Multi%2Ddimensional+arrays&Forum_Title=-
    C%2FC%2B%2B


    --
    Top ten Expert at Experts-Exchange


    Posted via http://dbforums.com
    Axter, Aug 19, 2003
    #5
    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. d[ - - ]b

    Multidimensional arrays? anything else?

    d[ - - ]b, May 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,672
    AlexS
    May 18, 2004
  2. Jay
    Replies:
    1
    Views:
    2,556
    BarryNL
    Jan 30, 2004
  3. geclinke
    Replies:
    1
    Views:
    5,054
    jackie
    Jun 18, 2004
  4. Philipp
    Replies:
    21
    Views:
    1,127
    Philipp
    Jan 20, 2009
  5. Francesco
    Replies:
    2
    Views:
    1,107
    Francesco
    Nov 6, 2009
Loading...

Share This Page