Dynamic Memory Allocation for Multi Dimensional array

I want to create a multi-dimensional array in runtime in as simple way as possible. so, i tried the following way...

1
2
3
int *rows;
//then i can always create an one dimensional array in the following way
rows= new (nothrow) int[a]; if (rows==0) {cout<<"Memory couldn't be allocated for matrix rows";} //( where a is a dynamic value.) 


so, i tried to extend the same idea to multidimensional array ain the following way
1
2
3
4
5
6
7
 
int **mat;
mat = new (nothrow) int * [b]; if (mat==0){cout<<" error message";}
/*it worked fine, i mean no syntax errors, but now i want to know what kind of structure does it creates...
one dimensional vector with a pointer (likely ) or a two dimensional array(very unlikely)
i want to use it as a two dimensional array... how can that be possible... pls help me...
*/ 


Pls help me... and pls do let me know if my idea is valid or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Matrix
{
      private:
              int *rows; // one dimensional array
              int *cols; // one dimensional array
              int **mat; // two dimensional array
              int i, j; //input value for the rows and matrix of the matrix...
      public:         
             Matrix()
             {}
             void setMatrix()
             {
                cout<<"Please input the Number of rows for the matrix:"; cin>>i;
                cout<<"Please input the Number of columns for the matrix:"; cin>>j; 
                rows= new (nothrow) int[i]; if (rows==0) {cout<<"Memory couldn't be allocated for matrix rows";}
                cols= new (nothrow) int[j]; if (cols==0) {cout<<"Memory couldn't be allocated for matrix columns";}
                mat = new (nothrow) int *[j]; if (mat==0){cout<<"memory allocation error";}
                getNewElements();
             }

             void getNewElements()
             {
             /* null */
              }
             
};

int main(void )
{
    Matrix a;
    getch();
    a.setMatrix();
    getch();
    return 0;
}
Last edited on
closed account (z05DSL3A)
Can you please just post your question in one place!

Ropex has started replying in this thread (for those of you interested):
http://www.cplusplus.com/forum/beginner/1390/
Last edited on
thank you grey wolf.
actually i posted it there... and later thought, since that was a beginners forum noone would reply me... and reposted it here... but i forgot to delete it tehre...

i am sorry...
Topic archived. No new replies allowed.