By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Creating Dynamic 2D Arrays

P: n/a
#define MAX_TABLE = 1024;
BYTE* dynamic1D = new BYTE[SIZE];

later..

i want to create a dynamic 2d ARRAY like this:

[0][<dynamic1D[1]>]
[0][<dynamic1D[2]>]
[0][<dynamic1D[3]>]
.....
[0][<dynamic1D[SIZE]>]

[1][<dynamic1D[1]>]
[1][<dynamic1D[2]>]
[1][<dynamic1D[3]>]
.....
[1][<dynamic1D[SIZE]>]

.....
[MAX_TABLE][<dynamic1D[1]>]
[MAX_TABLE][<dynamic1D[2]>]
[MAX_TABLE][<dynamic1D[3]>]
.....
[MAX_TABLE][<dynamic1D[SIZE]>]

Thx,
!MKD! ('',)!

Dec 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
monkeydragon schrieb:
#define MAX_TABLE = 1024;
BYTE* dynamic1D = new BYTE[SIZE];

later..

i want to create a dynamic 2d ARRAY like this:

Hi,
int main () {
int rows=10,cols=10;
int** mat = new int*[rows];
for (int i=0; i<rows; i++) mat[i]=new int [cols];

//access example:
mat[2][3]=15;

//dont forget to free it after its not required any more
for (int i=0; i<rows; i++) delete[] mat[i];
delete[] mat;

return 0;
}

Another way would be using STL:

vector <vector<int>> mat (rows,vector<int>(cols));
mat[2][3]=15;

regards marcas
Dec 15 '05 #2

P: n/a
marcas wrote:
monkeydragon schrieb:
#define MAX_TABLE = 1024;
BYTE* dynamic1D = new BYTE[SIZE];

later..

i want to create a dynamic 2d ARRAY like this:

Hi,
int main () {
int rows=10,cols=10;
int** mat = new int*[rows];
for (int i=0; i<rows; i++) mat[i]=new int [cols];

//access example:
mat[2][3]=15;

//dont forget to free it after its not required any more
for (int i=0; i<rows; i++) delete[] mat[i];
delete[] mat;

return 0;
}

Another way would be using STL:

vector <vector<int>> mat (rows,vector<int>(cols));
mat[2][3]=15;

regards marcas


I recommend the vector method, but be carefull to add a space betwee >>
Should be the following:
vector <vector<int> > mat (rows, vector<int>(cols));

With the current C++ standard, you'll get a compile error if you don't
put the space between > >. They are considering changing this in a
future version of the standard.

Also take a look at the following links for other methods for creating
a dynamic 2D array:
http://code.axter.com/dynamic_2d_array.h
http://www.codeguru.com/forum/showthread.php?t=231046
http://www.codeguru.com/forum/showth...hreadid=297838

C-Style compatible method:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c

Dec 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.