#### Archived

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

# Dynamically allocating multi-dimensional arrays...

This topic is 6204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im working on my map editor, but I''m stumped when it comes to allocating dynamic multidemsional arrays. I want to do a drop down list of different sized maps, and then generate an array according to the chosen size. So, given that I need to generate an array with the following 3 things describing the array: map[width][height][layers] or width,height and number of layers each one is an int... what would the c++ code look like? Thanks for any help. "Kiss my Converse!" -Shogun of Harlem -Barry Gordon''s "The Last Dragon"

##### Share on other sites

Here is one way to do it. I''ll present C code, but you can easily modify it to C++.

  // initial creationint width = ;int height = ;int layers = ;int* map = (int*)malloc(width*height*layers);// to reference an elementint z = ;int y = ;int x = ;int element = map[z*width*height + y*width + x]; // to change the size (this will trash the map)if(map) free(map);int width = ;int height = ;int layers = ;int* map = (int*)malloc(width*height*layers);

There are other ways to do it, such as creating an array of pointers to arrays, but this is harder to setup. If you make sure the width and height of the map are powers of two, you can access an element very quickly using bit-shifting using the above method.

Let''s say the width is 256 (2^8) tiles, and the height is 128 (2^7) tiles. To access an element, you can do:

int element = map[ (z<<15) + (y<<8) + x ];

##### Share on other sites
Here''s the code I use not the prettiest and its C++ I''ll just throw out my whole class here for ya least the parts you want
  class Tile{public: unsigned int id; char width; char height; int flags; char *extra;};class Map{public: Tile ***tiles; int width, height,layers;public: ~Map(); Allocate();};Map::Allocate(){ tiles = new Tile **[sizeof(Tile**) * layers]; for(int lyr = 0; lyr < layers; lyr++) { tiles[lyr] = new Tile *[sizeof(Tile*) * height]; for(int y = 0; y < height; x++) { tiles[lyr][y] = new Tile [sizeof(Tile) * width]; } }}//Free MemoryMap::~Map(){ for(int z = 0; z < layers; z++) { for(int y = 0; y < height; x++) { free(tiles[z][y]); } free(tiles[z]); } free(tiles);}

in your program just do something like Map map;
then when you want just map.width = 20; map.height = 50; map.layer = 3; then just map.Allocate(); and walla ya got it to access it you can use something like map.tiles[layers][y][x] to access any elements of the class tiles you can also replace tiles with like unsigned char or whatever just make sure you changed the sizeof in the allocate too a pretty simple way to make a dynamic 3d array Hope that helps need any more info just drop a line to derilect@hotmail.com

##### Share on other sites
Thanks guys, I really apreciate the help.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631780
• Total Posts
3002314
×