473,597 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3505
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
2822
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 generateVals() { ..... //generate some values
4
7670
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 simple, but its just not popping into my mind at the moment. My little snippet of code is below. Basically, the studentID array is dynamic so it will fit any length of a Student's Name. What I'm trying to do is place this chunk of code into a...
5
3402
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 be done using dynamic arrays. I don't know the size of the text file ahead of time, though, so I created a class that includes a method to resize the array. Here is that class: class Data { public: Data(int initialsize);
8
3672
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 static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
6
2970
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 strings'. I already know that no individual string will be longer than 50 characters. I just don't know before run time how many elements of the array will be needed. I have heard it is possible to dynamically allocate memory for a 2
25
2472
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 some of those advices like following? a. keep the interface clean and clear (What does clean or clear mean and how to achieve that?). b. avoid using static variables in local function if possible. c. avoid using global variables for local function...
94
4683
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 that I found inside of a string. Any ideas?
20
2309
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
7685
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
7965
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7885
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5426
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2399
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.