Archived

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

Quick Question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

My program needs to read a map data file and make a dynamic array to store the map''s data. The first three numbers in the file describe the dimensions of the map (Layers, x, y). My question is, if a certain map has only 1 layer, and I am making the array like this:
int * MapData;
MapData = new * int[Layers];
for (int i = 0; i < Layers; i++)
{
MapData = new * int[x];
}
//etc (for y...)
 
Will there be a problem if the first array is made with only 1 element? I can work around it if there is a problem with an array with one element, but if this works, it would make programming it easier. Does anyone know about this? Thanks!

Share this post


Link to post
Share on other sites
I don''t see any problem with that, but...

MapData = new * int[Layers];


this doesn''t look right to me, what is the * for? I would use

MapData = new int[Layers];


Harry.

Share this post


Link to post
Share on other sites
Thanks!

I think that

MapData = new * int[Layers];

is right though, because I am making more arrays inside:

MapData = new * int[x];


MapData[i][j] = new int[y];


Both would be inside loops so I can make another array for each one.

I just thought of another question though. How much space is normally used for map data. If I had a map with 3 layers and was 50x50, that means 7500 ints, and I can''t remember offhand the sixe of an integer. This sounds like a lot of memory thoug. Is it?

Share this post


Link to post
Share on other sites
I might as well post my other question here now:

Since the set up I mentioned above is going to be three levels of pointers and things made with the new operator, do I have to go through each item individually to delete them? Like this:

    
for (int i = 0; i < Layers_Max; i++)
{
for (int j = 0; j < X_Max; j++)
{
for (int k = 0; k < Y_Max; k++)
{
delete MapData[i][j][k];
}
delete MapData[i][j];
}
delete MapData[i];
}
delete MapData;


Or is there a faster way than this?
Thanks again!

Edited by - sbrown on March 19, 2001 11:29:14 AM

Share this post


Link to post
Share on other sites
Oh well in that case I would have thought that to make Mapdata[x][y][z] it would be:

  
MapData = new (int **)[x];

for(int a=0; a<y; a++)
{ MapData[a] = new (int *)[y];
for(int b=0; b<z; b++)
MapData[a][b] = new int[z];
}


Anyway if the above is correct for the allocation, the deallocation is like this:

  
for (int i = 0; i < Layers_Max; i++)
{ for (int j = 0; j < X_Max; j++)
delete [] MapData[i][j];
delete [] MapData[i];
}
delete [] MapData;


Harry.

Share this post


Link to post
Share on other sites
Well, you _could_ always use a one dimensional array:

map_data = new int[layers * x * y];

Then to access it:

map_data[(y*map_width) + x + map_size] = TILE;

where map_size is map_width * map_height.

I don''t know if this will be faster that a 3D array... but I''m guessing so, especially if you made restrictions so that map_width had to be a power of 2, then you could just bit shift.

Deallocation, of course, would be like this:

delete [] map_data;

Well, even if it isn''t easier to read, it''s still easy to do.

Cheers,

Share this post


Link to post
Share on other sites