470,815 Members | 1,343 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

General Question -- Dynamic 2D Array

Hello, all:

Just a general question that's been bothering me. Suppose I'd like to
create a 2D array of integers -- not using the vector template -- but I need
to dynamically create the array as I do not know the dimensions.

How could I do this using the "new" operator?

For instance, suppose I need to create an array of size row x column, but I
don't know the values being passed into the function...

I've tried it this way...

MyFunc(int nRow, int nCol)
{
int *array = (int *)new int[nRow][nCol];
...
delete [] array;
}

This seems to work for me, but a friend has told me this is -- perhaps --
unwise to do it that way. Any suggestions?

P.S. If there are multiple ways, could you select the way that would allow
me to access an element in the array by the following... array[1][2] =
blagh; That way I could easily access an element.

P.P.S. How would I delete the array as well?

Thank you, all.
Apr 26 '06 #1
4 3363
Just declare the array as an array, not as a pointer:

int[,] array = new int[nRow][nCol]:
P.P.S. How would I delete the array as well?


You don't. You just let it go out of scope, and it will be garbage collected.
Apr 26 '06 #2
I should mention that this is C++.

"Guffa" wrote:
Just declare the array as an array, not as a pointer:

int[,] array = new int[nRow][nCol]:
P.P.S. How would I delete the array as well?


You don't. You just let it go out of scope, and it will be garbage collected.

Apr 26 '06 #3
> I should mention that this is C++.

We assumed you meant *managed* C++ or C++/CLI, since you posted to the
..NET newsgroup. If this is for native/unmanaged c++, you should
probably post to another newsgroup.

Apr 26 '06 #4
There are two ways I know of to deal with this problem. One of them is just
to create a 1D array and do the 2D index math yourself:

#define INDEX2D(col, row, cols) ((col) + (row) * (cols))

MyFunc(int nRow, int nCol)
{
int *array = new int[nRow * nCol];

// to index col x, row y:
int n = array[INDEX2D(x, y, nCol)];
...
delete [] array;
}

The other way is to create a class to encapsulate a 1D dynamic array with an
overloaded operator[]. Template classes work really well for this. Then you
could create the 2D array like this:

MyDynArrayClass< MyDynArrayClass<int> > array;

and you would be able to index it like a regular array like this:

array[x][y];

"Saul775" wrote:
Hello, all:

Just a general question that's been bothering me. Suppose I'd like to
create a 2D array of integers -- not using the vector template -- but I need
to dynamically create the array as I do not know the dimensions.

How could I do this using the "new" operator?

For instance, suppose I need to create an array of size row x column, but I
don't know the values being passed into the function...

I've tried it this way...

MyFunc(int nRow, int nCol)
{
int *array = (int *)new int[nRow][nCol];
...
delete [] array;
}

This seems to work for me, but a friend has told me this is -- perhaps --
unwise to do it that way. Any suggestions?

P.S. If there are multiple ways, could you select the way that would allow
me to access an element in the array by the following... array[1][2] =
blagh; That way I could easily access an element.

P.P.S. How would I delete the array as well?

Thank you, all.

May 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Vasileios Zografos | last post: by
4 posts views Thread by Scott Lyons | last post: by
5 posts views Thread by meyousikmann | last post: by
8 posts views Thread by Peter B. Steiger | last post: by
6 posts views Thread by Materialised | last post: by
25 posts views Thread by lovecreatesbeauty | last post: by
20 posts views Thread by Cyn | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.