[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Dynamic 2-dimensional array

Posted on 2003-10-30
7
Medium Priority
?
494 Views
Last Modified: 2010-04-01
I am trying to declare a 2-dimensional array at runtime.  I can declare a 1 dimensional array as follows:

unsigned int * MyVariable;
MyVariable = new unsigned int[12];

However, the following does not work.

unsigned int * MyVariable;
MyVariable = new unsigned int[12][8];

I am using Microsoft Visual C++ 6.0 if it is of any help.

If it is an overloading solution some code would be useful.

Many thanks in advance.
0
Comment
Question by:reidy_boy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 14

Accepted Solution

by:
Farzad Akbarnejad earned 200 total points
ID: 9652130
Hi,


      unsigned int * MyVariable1;
      MyVariable1 = new unsigned int[12];

      // ------------------------------------------

      unsigned int ** MyVariable2;
      MyVariable2 = new unsigned int * [8];
      for (int i = 0; i < 11; i++)
            MyVariable2[i] = new unsigned int [8];


-FA
0
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 9652143
Hello,
Sorry change

MyVariable2 = new unsigned int * [8];

with

MyVariable2 = new unsigned int * [12];

-FA
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9652262
int **a ;

a = new int * [10] ;
for ( int i = 0 ; i < 10 ; i ++ )
  a [i] = new int [20] ;

This would get a array same as
a[10][20]

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 9654864
You can use the following class to dynamically create a 2 dimensional array:

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;
};
0
 
LVL 30

Expert Comment

by:Axter
ID: 9654882
Example usuage:
            dynamic_2d_array < int > dyn_2dArray(Q_Col, Q_Row);
            StartTime = GetTickCount();
            for (int t = 0;t < QtyTestItera;++t)
            for(int i0 = 0;i0 < Q_Col;++i0)
            {
                  for(int i1 = 0;i1 < Q_Row;++i1)
                  {
                        dyn_2dArray[i0][i1] = i0 + i1;
                  }
            }

Check out following link for more details on other methods for creating a dynamic two dimensional array:
http://www.axter.com/faq/topic.asp?TOPIC_ID=60&FORUM_ID=4&CAT_ID=9
0
 
LVL 15

Expert Comment

by:efn
ID: 9655746
When you allocate an array of objects of some type T, you get a pointer to T.  With

new unsigned int[12][8]

you are allocating an array of 8-integer arrays, so you get a pointer to an 8-integer array, which is not the same as a pointer to an integer.  So all you have to do is declare MyVariable like this:

unsigned int (* MyVariable)[8];

The parentheses are necessary to get a pointer to an array, rather than an array of pointers.

This only works if the 8 is a constant, as shown in your question.  If it's a variable, see the other answers.

--efn
0
 
LVL 1

Author Comment

by:reidy_boy
ID: 9656684
Thank you experts for your comments.  All are good answers, but I have given the points to FarzadA because it was the simplest.  Sys_Prog, your answer was along the same lines as FarzadA but he got there first!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question