Advertisement

multidimensional arrays

Started by February 25, 2002 02:26 PM
20 comments, last by evilclown 22 years, 9 months ago
How do I make multidimensioned arrays? I know how to do 1 dimension
  
//make an array that is array[sizex][sizey]

int* array = new int[sizex];

//now how make the sizey part?


//and when done, delete

delete [] array;
  
A 3 dimensional array.
  int* array = new int[sizex][sizey][sizez];  


,Jay
Advertisement
Although thats a pointer to a 3D array.

  int Array[x][y][z];  


is how to declare an actual array.

,Jay
quote: Original post by Jason Zelos
A 3 dimensional array.
      int* array = new int[sizex][sizey][sizez];  



You might like to consider checking this code.

--

It is against the law to stare at the mayor of Paris.

Edited by - SabreMan on February 25, 2002 3:54:00 PM
SabreMan, do you know how to do it?
you could

  // the linear 2d arrayint *array = new int[sizex*sizey];// access by doingarray[x+y*sizex];delete [] array;//// for a planar multidimensional array// int **array = (int**)(new int[sizey]);for(i=0; i<sizey; i++)   array[i] = new int[sizex];// access byarray[y][x];// deletionfor(i=0; i<sizey; i++)   delete [] array[i];delete [] array;// the c way// linear 2d arrayint *array = (int*)malloc(sizex*sizey);//deletionfree(array);// planar 2d arrayint **array = (int**)(malloc(sizey));for(i=0; i<sizey; i++)   array[i] = (int*)(malloc(sizex));// access byarray[y][x];// deletionfor(i=0; i<sizey; i++)   free(array[i]);free(array);  


saberman, how would you do such a thing, since you obviously know (by your comment about the code being wrong). since either one is a mulitdimensional array. there may be a better way to allocate planar arrays in c++, i am not sure since i rarly use planar arrays and tend to use c methods of allocation (except for objects).
Advertisement
I knew the syntax was illegal.

Dynamically allocating multi-dimensional arrays actually takes quite a lot of explanation, so I'm going to defer to someone who's already written up a perfectly good explanation, Marshall Cline...

How do I allocate multidimensional arrays using new?

Pointers and arrays are evil!

Do these links help?




Edited by - SabreMan on February 26, 2002 7:14:36 AM
So, is this right?
    vector< vector<int> > array;  Because I want to use a multidimensioned container of tiles      struct tile{int id;int walk;};  But don't understand how. If the above is right I think I would do      for(i = 0; i < map_width; i++) {  for(j = 0; j < map_height; j++) {    //initialize each tile so I have an    //array of tiles WIDTHxHEIGHT    tile tmp;    tmp.id = 0;    tmp.walk = 1;    map[i].push_back(tmp);  }}  

Do you have any links where I can learn about STL? I don't know anything about it. [/source]

Edited by - evilclown on February 26, 2002 3:33:06 PM
Something like this...
  int ***number_cube = new (int**)[5];for(int i = 0; i < 5; ++i) {    number_cube[i] = new (int*)[5];    for(int j = 0; j < 5; ++j) {        number_cube[i][j] = new int[5];    }}...for(int i = 0; i < 5; ++i) {    for(int j = 0; j < 5; ++j) {        delete[] number_cube[i][j];    }    delete[] number_cube[i];}delete[] number_cube;  
I''d do it like this using C style syntax.

  struct tile {    int id;    int walk;}; tile TileMap[map_width][map_height];for(i = 0; i < map_width; i++) {      for(j = 0; j < map_height; j++) {         TileMap[i][j].id = 0;          TileMap[i][j].walk = 1;      }}  



I think this is a more conventional way of doing it, if not easier to think about.

I''m going to have to talk to my eyes about reading posts all the way through before replying .

,Jay

This topic is closed to new replies.

Advertisement