how to new char* array[MAX][ANOTHER_MAX]

how to new char* array[MAX][ANOTHER_MAX]

Post by Someo » Wed, 30 Jul 2003 07:12:07



This is legal:
        const int MAX = 5;
        char* test = new char[MAX];

Now, how do I new a multidimensional array, like 'array[MAX][MAX2]'?

Thanks!
Sean Rowe

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

how to new char* array[MAX][ANOTHER_MAX]

Post by llewell » Thu, 31 Jul 2003 07:48:04



> This is legal:
>    const int MAX = 5;
>    char* test = new char[MAX];

> Now, how do I new a multidimensional array, like 'array[MAX][MAX2]'?

http://www.boost.org/libs/multi_array/doc/user.html#sec_example

is one example. There are other multi-dimensional array classes out
    there. You could brew your own.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

how to new char* array[MAX][ANOTHER_MAX]

Post by Jon Hein » Thu, 31 Jul 2003 17:20:27


 > This is legal:
 >       const int MAX = 5;
 >       char* test = new char[MAX];
 >
 > Now, how do I new a multidimensional array, like 'array[MAX][MAX2]'?

you could do:   char * test = new char[MAX * MAX2];

you may want to seriously reconsider what you are doing however.
multidimensional arrays are a common source of problems, especially
with beginners, and they often do not behave as expected. there are
several different containers that may do the job you need. [ vectors
of vectors, valarrays + slices, etc... ]

Stroustroup has an excellent discussion of the pitfalls and
considerations inherent with multi-dimensional arrays. See Section C.7
in his "The C++ Programming Language".

regards,
jon

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

how to new char* array[MAX][ANOTHER_MAX]

Post by Francis Glassboro » Fri, 01 Aug 2003 09:02:51




Quote:> > This is legal:
> >       const int MAX = 5;
> >       char* test = new char[MAX];

> > Now, how do I new a multidimensional array, like 'array[MAX][MAX2]'?

>you could do:   char * test = new char[MAX * MAX2];

Well you could but this code works fine (and the typedef is syntactic
sugar that makes the code easier to write.):

int const max1(5);
typedef char ar[max1];
int const max2(7);

ar* array = new ar[max2];

int main() {
  array[2][3] = 'a';
  cout << array[2][3];

Quote:}

--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow      ACCU

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

how to new char* array[MAX][ANOTHER_MAX]

Post by Dhru » Fri, 01 Aug 2003 09:11:06



> This is legal:
>    const int MAX = 5;
>    char* test = new char[MAX];

> Now, how do I new a multidimensional array, like 'array[MAX][MAX2]'?

The 2nd parameter needs to be fixed:

suppose, you want an array of, say [n][50]

do this:

int n;
std::cin>>n

int (*p)[50] = new int [n][50];

or better, use this:

std::vector <vector<int> > array_2d;
                        ^ //NOTE the extra space.

HTH,
-Dhruv.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

how to new char* array[MAX][ANOTHER_MAX]

Post by fo » Fri, 01 Aug 2003 21:49:57


 > This is legal:
 >       const int MAX = 5;
 >       char* test = new char[MAX];
 >
 > Now, how do I new a multidimensional array, like 'array[MAX][MAX2]'?

Check out the code in the following link:

http://www.axter.com/faq/topic.asp?TOPIC_ID=60&FORUM_ID=4&CAT_ID=9&To...
2B%2B

The dynamic_2d_array class listed in the above link can be used to
dynamically create a 2 dimensional array.
It can also be used to statically create a two dimensional array.
The class is very compatible with a C-Style static 2 dimensional
array.

Example usage:
dynamic_2d_array < char > My_dynamic_2d_array(3, 20);
char TestData[3][20] = {"hello world", "bla bla","more bla"};
int i;
for (i = 0;i < 3;++i)
{
   for (int i2 = 0;i2 < 20;++i2)
   {
     My_dynamic_2d_array[i][i2] = TestData[i][i2];
   }

Quote:}

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 
 
 

how to new char* array[MAX][ANOTHER_MAX]

Post by fo » Sat, 02 Aug 2003 10:05:12




> > This is legal:
> >       const int MAX = 5;
> >       char* test = new char[MAX];

> > Now, how do I new a multidimensional array, like 'array[MAX][MAX2]'?

> The 2nd parameter needs to be fixed:

> suppose, you want an array of, say [n][50]

> do this:

> int n;
> std::cin>>n

> int (*p)[50] = new int [n][50];

> or better, use this:

> std::vector <vector<int> > array_2d;
>                    ^ //NOTE the extra space.

> HTH,
> -Dhruv.

The class listed below can create a 2 dimensional array dynamically.
This method is more compatible to a C-Style 2 dimensional array then
using the vector in vector method, and it's better optimized.

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])
        {//The following code prevents the default constructor from being
used without defineing ROW_T and 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;

Quote:};

See following link for usage:

http://axter.com/faq/topic.asp?TOPIC_ID=60&FORUM_ID=4&CAT_ID=9&Topic_...

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]