Archived

This topic is now archived and is closed to further replies.

Sand_Hawk

Dynamic Multi Dimensional arrays

Recommended Posts

I am busy with an app that requires multi dimensional arrays, However, since they differ per object I create them dynamicly. This is the source I tried:
  
char *Data;
Data = new char[2, 10]; // This would create an array with 2 row and 10 chars per row

// do stuff

delete [] Data;
  
Why doesn''t this work? I didn''t test it on a VC6 compiler but on Borland but that shouldn''t be the problem. I never tried this source at home but can anyone tell me what I might do wrong? Sand Hawk ---------------- -Earth is 98% full. Please delete anybody you can.
My Site

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try this:

char *Data;
Data = new char[2][10];

// Do stuff

delete [] Data;

Share this post


Link to post
Share on other sites
Hi Sand_Hawk,

I don''t have a lot of experience with c++ but i think this is your solution:

#define length1 10
#define length2 2
char** data;

data = new char*[length1];
for (i = 0; i < length1; ++i)
data = new char[length2];


In c++, real multi dimensional arrays doesn''t exist. Instead you can use an array of array''s.

Glenn

Share this post


Link to post
Share on other sites
Thanks for all the help so far. I''ve used this method:


char** data;

data = new char*[length1];
for (i = 0; i < length1; ++i)
data = new char[length2];


I can write to the memory, but if I try to read it my programs crahes. I use Data[Y][X] to read 1 character.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site

Share this post


Link to post
Share on other sites
I must have posted this a million times over!


    
typedef float ARRAY[3]; // Just as an example.


ARRAY *TwoDArray;

TwoDArray = new ARRAY[3];

// And to delete...

delete [] TwoDArray;


This should create aand delete a 3x3 array.

Peace out,
Mathematix.
Q3A + UT1999 = UT2003
therefore UT2003 - Q3A = UT1999
and UT2003 - UT1999 = Q3A!

[edited by - Mathematix on November 7, 2002 5:18:54 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Sand_Hawk
Thanks for all the help so far. I've used this method:


char** data;

data = new char*[length1];
for (i = 0; i < length1; ++i)
data = new char[length2];


I can write to the memory, but if I try to read it my programs crahes. I use Data[Y][X] to read 1 character.



Here's the problem. It should be:

  
char** data;

data = new char*[length1];
for (i = 0; i < length1; ++i)
data[i] = new char[length2];


Note that each element in data is now getting a 1d array, making a 1d array of 1d arrays: ie a 2d array.

Mathematix's idea looks pretty neat, though.

Peace,
Doc


[edited by - doc on November 7, 2002 6:40:41 PM]

[edited by - doc on November 7, 2002 6:42:02 PM]

Share this post


Link to post
Share on other sites
I noticed I made a mistake in pasting the code. I used that code. However, coming back to the typedef, can I use a typedef to when I want to create dynamic X and Y axis because now you set it to max 3 on the rows.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site

Share this post


Link to post
Share on other sites
Nope. That typedef will be fixed. Dynamic sized columns will have to be done the hard way.

Peace,
Brendon

Share this post


Link to post
Share on other sites
It seems like I am going crazy. I ran the debugger over it and it should work. Here is the code:


  
int iIndex = 0;
ifstream FishFile(FileName, ios::binary);

// Load X and Y dimensions from the file

FishFile.read((char *)&DimX, sizeof(int));
FishFile.read((char *)&DimY, sizeof(int));

// Allocate dynamic array with size of the fish

FishData = new char*[DimY];
for (iIndex = 0; iIndex < DimY; iIndex++)
FishData[iIndex] = new char[DimX];

// Read the fish from file

FishFile.read((char *)FishData, sizeof(FishData));


It is for the Ascii fishtank contest. What is wrong with the allocation code? And also, can I read the array from file the way I do it?

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site

Share this post


Link to post
Share on other sites
Is it me or has this question come up a thousand times in the past week, and every time people give the same answer and everytime I''m forced to contradict them.

Don''t allocate "multidimensional arrays" in the manner that these people claim. Instead, allocate all of the elements in one strip (instead of each element being a pointer to some other place in memory that''s allocated separately).

IE, for a 5 x 4 dimensional array, allocate an array of 20 elements, then, when you refer to elements multiple the row you want to access by the number of colums and add the row you want to access to get the element in the array that you need.

So:

char* Array;
Array = new char[20]; // 5 x 4
//To access the 3rd row 2nd colum do
Array[ 3 * 4 + 2 ];

This is how multidimensional arrays are allocated and accessed internally when you''re not dynamically allocating them, it''s just hidden from you.

Doing it the other way (array of pointers) forces a longer construction, a longer destruction, memory fragmentation, different pointer arithmetic from non-dynamically allocated arrays, and even more code.

If you don''t like having to access data like that, then just encapsulate everything into a class and overload operator() to do it for you.

Share this post


Link to post
Share on other sites
Yes, that is a better way to do it, but I thought the question was how do you make it so that each row has a different number of columns. Depending on the application, it may or may not be worth having the ease of the same number of columns in each row.


Sand_Hawk, this:

FishFile.read((char *)FishData, sizeof(FishData));

is disasterous. FishData is a pointer to pointers, not variables. What your doing is reading the data from the file and then using it to point to where the actual FishData elements are contained. What you want to do is to read each element into its proper place. Two ways:
Do it one by one, ie:

    
for (y = 0; y < ylen; ++y) {
for (x = 0; x < xlen; ++x) {
FishFile.read(&(FishData[x][y]), sizeof(whatever type the element is));
}
}

You may need to swap that [x][y] for [y][x], i don't know for sure.

Or you could read entire rows in:

for (y = 0; y < ylen; ++y) {
FishFile.read(FishData[y], the length of that row);
}



If you don't really need to have row with all different numbers of columns in each, don't bother. Just use one straight line of memory then calculate where a 2D point would be: x + y * width.

Peace,
Brendon

EDIT: Formatting


[edited by - doc on November 8, 2002 10:12:31 PM]

Share this post


Link to post
Share on other sites