Archived

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

HellRiZZer

Dynamically allocate/deallocate 3 dim array on floats..

Recommended Posts

How do I dynamically allocate and de allocate a 3 dimension array of floats? e.g I need controlpoints[4][4][3], and then need to destroy them.. please help..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The simpliest way is to create a one-dimensional-array. Then you can use a macro to access the element you want or the "()" operator if you use c++.

Share this post


Link to post
Share on other sites
Here''s how I do it with a lil tile engine I''m working on
  
tiles = new Tile **[sizeof(Tile**) * layers];
height_map = new UCHAR *[height];

for(int lyr = 0; lyr < layers; lyr++)
{
tiles[lyr] = new Tile *[sizeof(Tile*) * height];

for(int x = 0; x < height; x++)
{
height_map[x] = new UCHAR [width];
tiles[lyr][x] = new Tile [sizeof(Tile) * width];
}

}

just change the tiles to floats and the UCHAR to float and such

Share this post


Link to post
Share on other sites
Allocation:
float ***controlpoints;
controlpoints = new float**[4];
for(int x = 0; x < 4; ++x)
{
controlpoints[x] = new float*[4];
for(int y = 0; y < 4; ++y)
controlpoints[x][y] = new float[3];
}

Deallocation:
for(int x = 0; x < 4; ++x)
{
for(int y = 0; y < 4; ++y)
// delete innermost arrays
delete [] controlpoints[x][y];
// delete outer inner arrays
delete [] controlpoints[x];
}
// delete outermost array
delete [] controlpoints;

Share this post


Link to post
Share on other sites