473,394 Members | 1,765 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,394 software developers and data experts.

C++ multidimensional array allocation using new []

1
I have a problem. I need to dynamically allocate variable a, not in initialization but later in the program (i.e. based upon input data). Actually, I have 2-dimensional array the first dimension unknown, and I want to change just the first dimension (other dimensions are known in declaration). I heard this is possible but I don't know how.
So in the beginning I have the following:
int a[][5];
And somehow I need to get a[10][5];

..Or the problem in 3 dimensions:
I need to get a[15][10][5] from previous declaration of a[][10][5] (or ***a for that matter).

How to do that?
Sep 17 '07 #1
5 3048
Ganon11
3,652 Expert 2GB
You'll need to use a for...loop for each dimension. What I do when faced with this type of problem is this:

1) Declare your array as an n-pointer (where n is the number of dimensions you need), but don't allocate memory just yet.
2) Get the size variable from the user.
3) Initialize the array as an array (with the size given by the user) of (n-1)-pointers.
4) For each member in this array,
4) a. Initialize this (n-1)-pointer as an array of (n-2)-pointers.
4) b. For each member in this array,
4) b. I...(repeat until you have an array of ints, or doubles, or whatevers).

So, for a 2-d array, this becomes

Expand|Select|Wrap|Line Numbers
  1. int **array, size;
  2. //Get user input into size;
  3. array = new int*[size];
  4. for (int i = 0; i < size; i++)
  5.    array[i] = new int[10];
  6.  
  7. // array is now a 2D array with dimensions size and 10.
Sep 17 '07 #2
RRick
463 Expert 256MB
Or you can use C++ vectors to define multiple arrays. It can get messy and typedefs are handy for the definitions. For example,
Expand|Select|Wrap|Line Numbers
  1. typedef vector<int> Ind1d;
  2. typedef vector< Vec1D> Int2d;
  3. ...  until the cows come home ....
  4.  
Since these definitions create empty vectors, you'll still have to initialize the various vector dimensions as you would in creating arrays with new [].
Expand|Select|Wrap|Line Numbers
  1. //  Initialize 2D vector to [10][50]
  2. Int2d iarray;
  3. iarr.reserve( 10);
  4. for ( int ind=0; ind<10; ind++)
  5.     iarray[ ind].capacity( 50):
  6.  
Now you can access entries like you expect.
Expand|Select|Wrap|Line Numbers
  1.  iarray[5][35] = 99;
The real advantage of this is in the allocation and deleting of the vector. Once you delete iarray, all of the entries for all dimensions are gone.
Sep 18 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Or perhaps, you could just use an array properly.

First, there are only one-dimensional arrays in C or C++. The number of elements in out between brackets:
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
That is an array of 5 elements each of which is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[];
  2.  
won't compile. You need to declare the number of elements.

Second, this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
is still an array of 5 elements. Each element is an array of 10 int.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10][15];
  2.  
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


Expand|Select|Wrap|Line Numbers
  1. int array[][10];
  2.  
won't compile. You need to declare the number of elements.

Third, the name of an array is the address of element 0
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
  3. int (*ptr)[10] = array;
  4.  
Fourth, when the number of elements is not known at compile time, you create the array dynamically:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value][5];
  2. int (*ptr)[10] = new int[value][10];
  3. int (*ptr)[10][15] = new int[value][10][15];
  4.  
In each case value is the number of elements. Any other brackets only describe the elements.

Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

Expand|Select|Wrap|Line Numbers
  1. int** ptr = new int[value][10];    //ERROR
  2.  
new returns the address of an array of 10 int and that isn't the same as an int**.

Likewise:
Expand|Select|Wrap|Line Numbers
  1. int*** ptr = new int[value][10][15];    //ERROR
  2.  
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
Sep 18 '07 #4
Suko
6
Hi, not sure which compiler your using but Borland C++ and Delphi use a class called DynamicArray with member func's to do all u need.
Sep 18 '07 #5
Suko
6
Hello,

sorry but im not sure about this :-

int (*ptr)[10] = new int[value][10];
int (*ptr)[10][15] = new int[value][10][15];

Can you explain what 'value' is exactly and how it affects the array?
In the second declaration its an array of 10 ints containing 15 int elements each, but what does 'value' do?
Sep 18 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Wihlelm Bierbaum | last post by:
I'm trying to make use of a certain method of a COM object that takes a multidimensional array as its sole parameter. Here's an example of the construction of said array: $cA = array(); $cA =...
1
by: ip4ram | last post by:
I have an old set of library,where I used malloc to allocate multidimensional(2 and 3) arrays,using only one malloc call(using void pointers).I get the size of the array in run time.Now that I am...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
4
by: Gregory.A.Book | last post by:
I'm working with displaying and manipulating very large image sets. The program handles anything from 2D images to 4D RGB volumes in a time-series. I've been using dynamically allocated arrays to...
18
by: welch.ryan | last post by:
Hi all, Having a problem with addressing large amounts of memory. I have a simple piece of code here that is meant to allocate a large piece of memory on a ppc64 machine. The code is: /*...
14
by: Michel Rouzic | last post by:
Hi, I've recently met issues with my program which can only be explained by heap corruption, so I've tried debugging my program with Valgrind, and here's what I get with the following...
3
by: luftikus143 | last post by:
Hi there, I need to store three pieces of data - a result of a SQL query - in a multidimensional array, but don't really succeed. Sometimes it works, but then the output doesn't work accordingly....
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.