Archived

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

Smorg123

3 dimensional arrays

Recommended Posts

Smorg123    122
I''m sure someone has asked this type of question before. How can I make a dynamic 3 dimensional array. This is what I''m trying, but the program crashes if I try inserting values throughout the array.
GLuint ***data;

data = new GLuint **[length];

for (int i=0; i = new GLuint *[width];

for (i=0; i   

Share this post


Link to post
Share on other sites
LessBread    1415
quote:
Original post by Smorg123
I''m sure someone has asked this type of question before.
How can I make a dynamic 3 dimensional array.
This is what I''m trying, but the program crashes if I try inserting values throughout the array.




  
GLuint ***data;

data = new GLuint **[length];

for (int i=0; i<length; i++)
data[i] = new GLuint *[width];

for (i=0; i<length; i++)
for (int j=0; i<width; i++)
data[i][j] = new GLuint [3];


{ source } ... { /source } - substitute [ and ] for { and }

Looks like you can consolidate loops there.


  
GLuint ***data;

data = new GLuint **[length];

for (i=0; i<length; i++)
data[i] = new GLuint *[width];
for (int j=0; i<width; i++)
data[i][j] = new GLuint [3];


3d arrays are a really pain - you might want to consider taking a different approach altogether.



"Beautiful maiden," answered Candide, "when a man is in love, is jealous, and has been flogged by the Inquisition, he becomes lost to all reflection."

Share this post


Link to post
Share on other sites
Oluseyi    2112
quote:
Original post by Smorg123
This is what I''m trying, but the program crashes if I try inserting values throughout the array.

Do you really mean "inserting" or do you mean assigning? You can''t just insert new elements into an array in C or C++ - dynamic or otherwise. You need to perform reallocation. In C++ you do have the luxury of a class that provides this functionality for you and more: std::vector (in the <vector> header file).

Share this post


Link to post
Share on other sites
Mathematix    259
Try...


  
const int Y = 10;
const int Z = 10;

typedef GLuint GLUIarray[Y][Z];

GLUIarray *GLUIarray3D;

GLUIarray3D = new GLUIarray[10];

// Do your stuff.


delete [] GLUIarray3D;






Peace out,
Mathematix.

[edited by - mathematix on September 30, 2002 11:45:47 PM]

Share this post


Link to post
Share on other sites
Smorg123    122
I should explain the situation I''m trying to use it. I just want to have variable sized hieght map and store the rgb set for each point. I guess I could have a 2d array and store the rgb values sequentially, or have a 1d array and calculate the offset each time. Maybe 3 seperate 2d arrays would be better.

STL vector wont allow vector of a vector, a bit of a problem.

Either way a 3d array would look alot cleaner in my opinion.

Share this post


Link to post
Share on other sites
petewood    819
store the height and rgb as a pair
or have two 2d arrays height[m][n] and rgb[m][n]

i assume the coordinates are regular and related to the indices of the array

otherwise you''ll need to store the position as well

Share this post


Link to post
Share on other sites