473,322 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 3480
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
25
by: lovecreatesbeauty | last post by:
Could you talk something about the general rules on the interface (function) design in C program that recognized publically? Or introduce some good advice of yourself. How do you think about...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
20
by: Cyn | last post by:
Hi, I want to create a general array structure which can hold all types. Something like this: struct ARRAY { void **array; size_t size; };
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.