Dynamic arrays???
Hi all!
Recently i have been learning Isometric tiling and today
i started coding a map editor. Then a problem came to mind:
How the heck do i allocate the map array dynamically?
My tile structure looks like this:
struct Iso_Tile
{
int type; // type of tile
int attr; // Attributes of the tile
unsigned long x; // x position of tile
unsigned long y; // y position of tile
}
So far i have just declared the map like this:
Iso_Tile map[(height of map)][(width of map)];
But how do i do this dynamically??
to dynamic allocate a n - dimensional array, n>1, the other boundaries must be constant:
eg makeing a 4x4 array, one of them must be know at "compile time"
#include "stdlib.h"
struct my
{
int i;
char x;
};
int main(int argc, char* argv[])
{
my (*cp)[4];
int screeny = 4;
cp = new my[screeny][4];
cp[2][2].i = 5;
delete [] cp;
return 0;
}
the 4 in "my (*cp)[4];" and "cp = new my[x][4];" can't be replaced by a variable.
a way around this is to use single dimension array, and calulate the index.
int main(int argc, char* argv[])
{
my *cp;
int screenx = 4, screeny = 4;
cp = new my[screenx*screeny];
int elementx = 2, elementy = 2;
cp[ elementy * screeny + elementx].i = 5;
delete [] cp;
return 0;
}
and remember to check for failed allocation ( pointer returned == NULL )
Edited by - Claus Hansen Ries on June 13, 2000 6:49:59 AM
eg makeing a 4x4 array, one of them must be know at "compile time"
#include "stdlib.h"
struct my
{
int i;
char x;
};
int main(int argc, char* argv[])
{
my (*cp)[4];
int screeny = 4;
cp = new my[screeny][4];
cp[2][2].i = 5;
delete [] cp;
return 0;
}
the 4 in "my (*cp)[4];" and "cp = new my[x][4];" can't be replaced by a variable.
a way around this is to use single dimension array, and calulate the index.
int main(int argc, char* argv[])
{
my *cp;
int screenx = 4, screeny = 4;
cp = new my[screenx*screeny];
int elementx = 2, elementy = 2;
cp[ elementy * screeny + elementx].i = 5;
delete [] cp;
return 0;
}
and remember to check for failed allocation ( pointer returned == NULL )
Edited by - Claus Hansen Ries on June 13, 2000 6:49:59 AM
Ries
Thanks for the help Claus!
Only one question:
Why can''t i allocate the second dimension of the array?
Only one question:
Why can''t i allocate the second dimension of the array?
It is possible to have array bounds dynamic in all dimensions. The code just gets uglier.
Allocation:
Deallocation:
Edited by - SiCrane on June 13, 2000 10:15:20 AM
Allocation:
typedef struct Iso_Tile * pIso_Tile;typedef pIso_Tile * ppIso_Tile;ppIso_Tile map = new pIso_Tile[height];for (int a = 0; a < height; a++) { map[ a ] = new Iso_Tile[width];}
Deallocation:
for (int a = 0; a < height; a++) { delete [] (map[ a ]);}delete [] map;
Edited by - SiCrane on June 13, 2000 10:15:20 AM
why not:
just make sure the way you access a single cell: what is the column, what is the row...
struct tile
{
int type, attr;
char blah;
};
main( )
{
tile* theWorld; // worl ptr
// dynamic allocation
// read worldwidth & worldheight from whatever source
theWorld = new tile[ worldwidth * worldheight ];
// or even:
// theWorl = (tile*) malloc( worldwidth * worldheight * sizeof(tile) );
// to access tile [40][12]
theWorld[ 40*worldwidth + 12].type = 0;
delete theWorld;
// or free( theWorld );
}
just make sure the way you access a single cell: what is the column, what is the row...
If I''m not mistake, Jehan, you must call your delete like this:
delete[] theWorld;
Otherwise it''ll just delete the first element in your array.
delete[] theWorld;
Otherwise it''ll just delete the first element in your array.
- Houdini
allocate
Edited by - ncsu121978 on June 13, 2000 11:27:13 AM
Iso_Tile** myMap;myMap = new Iso_Tile[map_width];for (int i = 0; i < map_width; i++){ myMap<i> = new Iso_Tile[map_height];} [/source]now all you have to do to access a cell is just like you would a normal arraymyMap[width_position][height_position] = whatever;deallocate[source]for (int i=0; i<map_width; i++){ delete [] myMap[i];}delete [] myMap;
Edited by - ncsu121978 on June 13, 2000 11:27:13 AM
If you want dynamicness, use Linked lists, otherwise, you will just obfuscate your code so bad you will have to abandon it.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement