Archived

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

Dominik_78

3 Dimensional Array in CPP

Recommended Posts

Hi, I can''t find a good advice for that so I try it here. I need a 3Dim Array in CPP. Already know hoe to create it using:
typedef unsigned short** ppushort;
typedef unsigned short* pushort;

ppushort *dynarray;
So I create the Array doing:
dynarray = new ppushort[dim1count];
for (int i = 0; i < dim1count; i++) {
   dynarray[i] = new pushort[dim2count];
   for (int j = 0; j < dim2count; j++) {
      dynarray[i][j] = new unsigned short[dim3count];
   }
}
so fare so good. Know I would like to know: Because I do not need all the elements for dim2count and dim3count (just making sure that I allocate enough for all dim1count''s) 1. Can I allocate the dim2 and dim3 more dynamic - say let the array look like: dynarray[0][0][0] dynarray[0][0][1] dynarray[0][1][0] dynarray[0][1][1] dynarray[0][1][2] dynarray[0][2][0] dynarray[0][2][1] this would save me much memory - because of the amount of data! 2. If I can create the Array as I would like it to have - can I determine the size of one dimention (dynarray[0][0] = 2; dynarray[0][1] = 3; dynarray[0][2] = 2)? thanks for your help.

Share this post


Link to post
Share on other sites
If I understand you correctly, then no.

an array [x][y] will contain x sets of y columns of data.

so if you needed

1 1 1 1 1
2 2 . . .
3 3 3 3 .
4 4 4 4 4

and didn''t need the entries marked with a ., then you still have to allocate memory for them. (I know my example is 2d, but I am sure that you get the idea.)

For a 2d array, you could have an array of vectors, where each vector is a single row of data, and can be of variable length. (In fact you could have a vector of vectors to give a very dynamic 2d array.)

If you want a completely fluid 3d array, you could expand on this, and have a vector containing vectors of the 2d array, but my brain is starting to hurt now.

Bp.

Share this post


Link to post
Share on other sites
The default behavior of C/C++ will not let you change the size of one dimension across other dimensions. However, if you are careful about tracking memory, you can easily write an array class which allocates a heap of memory equal to dim1size * dim2size * dim3size * sizeof(data). You may even be able to use an overloaded [] operator, but you''ll most likely have to settle for an accessor function.

Share this post


Link to post
Share on other sites
thanks Bagpuss.

That's what I thought! Damit.
1. Dim about 400.
2. Dim max at 150 but most of the time about 1-4
3. Dim max at 200 but most of the time about 1-30

so i am wasting memory (about 30 MB surly)

so no good solution like a simple vector-class?
or can you point me to the concret name - I am in Watcom 11.0 for this. but there is surly a vector-class in it.
could a class be something like this:


class A {
private:
B *dim1;
}

class B {
private:
C *dim2;
}

class C {
private:
unsinged short val;
}


this would let me make classes and points to classes to my data? and if so? can i make that as i need it (you know the array you draw with .)

EDIT: BTW: I am doing this in a DLL. ;-)

[edited by - Dominik_78 on July 28, 2003 8:47:20 AM]

Share this post


Link to post
Share on other sites
...or make a 2-dimensional array of a class, and the class itself saves the 3rd dimension column - so you can "ask" the class, how long each 3rd dimension column is - but with this way your memory won''t be addressed at the same place and thinkgs like myarray[0][2][7] are dead then - but you can type this, if you make it this way:
myarray[0][2].GetMemory()[7], for example.

Share this post


Link to post
Share on other sites