Dynamic Multi Dimensional arrays

Started by
11 comments, last by Sand_Hawk 21 years, 5 months ago
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.
Advertisement
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]
My stuff.Shameless promotion: FreePop: The GPL god-sim.
boost::multi_array has been released in 1.29

See Boost link in sig.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement