How to create a dynamic sized map?
How do I create a map that can be changed in runtime?
You can use:
int *map;
map = new int[100];
Right? But how do you create a x and y map?
map[100][100]
(I read a thread about it here, but I can''t find it, sorry)
Let''s see, the last time I answered this question I found out I completely misunderstood how c implements multidimentional arrays. However, the following should be correct:
int *map;
map = new int*[100];
for(int i=0;i<100;i++)
map = new int[100];
then you should be able to use map like this:
map[x][y] = some_value;
int *map;
map = new int*[100];
for(int i=0;i<100;i++)
map = new int[100];
then you should be able to use map like this:
map[x][y] = some_value;
- Ryan -
No, you'll need to delete all the pointers map points to first.
for(int i=0;i<100;i++)
delete []map(i); // Replace () with []
delete map;
Then recreate
Edited by - RMack on 2/21/00 9:55:57 AM
Edited by - RMack on 2/21/00 9:57:30 AM
for(int i=0;i<100;i++)
delete []map(i); // Replace () with []
delete map;
Then recreate
Edited by - RMack on 2/21/00 9:55:57 AM
Edited by - RMack on 2/21/00 9:57:30 AM
- Ryan -
The complete code as I always do it is like this..
// Creation of table:
int** map;
map = new (int*)[ 100 ];
for ( int i = 0; i < 100; i++ )
{
map = new int[ 100 ];<br>}<br><br>// Use the table;<br><br>map[ x ][ y ];<br><br><br>// Deleting of table:<br><br>for ( int i = 0; i < 100; i++ )<br>{<br> delete [] map;<br>}<br>delete [] map;<br><br>Ofcourse you should always check your news on NULL or do exception handling on them.<br><br>Jaap Suter <br><br>Mmmm, I''ll have to think of one.
// Creation of table:
int** map;
map = new (int*)[ 100 ];
for ( int i = 0; i < 100; i++ )
{
map = new int[ 100 ];<br>}<br><br>// Use the table;<br><br>map[ x ][ y ];<br><br><br>// Deleting of table:<br><br>for ( int i = 0; i < 100; i++ )<br>{<br> delete [] map;<br>}<br>delete [] map;<br><br>Ofcourse you should always check your news on NULL or do exception handling on them.<br><br>Jaap Suter <br><br>Mmmm, I''ll have to think of one.
____________________________Mmmm, I''ll have to think of one.
My error mst be with using int *map instead of int **map.
Sorry ZomeonE
Sorry ZomeonE
- Ryan -
I would like to pitch in here. You now know how to use a dynamic two-dimensional array. I would like to teach how to use a dynamic one-dimensional array AS a two-dimensional array. Why? you ask, because it is faster and it is simpler.
You declare and allocate the map like this:
unsigned int Width = 10, Height = 10;
int *Map = new int[Width * Height];
And you refer to each element like this:
int Element = Map[x + y*Width];
When you''re finished you free the memory like this:
if( Map != 0 )
{
delete[] Map;
Map = 0;
}
Why is this faster? Because with two-dimensional arrays the program must use two steps to get to the actual data, first find the right row array and then find the data in it. With a one-dimensional array it just grabs the data right from the first array. Yes, you do a little more aritmatic, but you skip one pointer lookup which is much slower than adding an multiplying. Besides with two-dimensional arrays there are no guarantee that the whole map lies in the same memoryblock which can result in more memory flipping in and out of the cache.
You declare and allocate the map like this:
unsigned int Width = 10, Height = 10;
int *Map = new int[Width * Height];
And you refer to each element like this:
int Element = Map[x + y*Width];
When you''re finished you free the memory like this:
if( Map != 0 )
{
delete[] Map;
Map = 0;
}
Why is this faster? Because with two-dimensional arrays the program must use two steps to get to the actual data, first find the right row array and then find the data in it. With a one-dimensional array it just grabs the data right from the first array. Yes, you do a little more aritmatic, but you skip one pointer lookup which is much slower than adding an multiplying. Besides with two-dimensional arrays there are no guarantee that the whole map lies in the same memoryblock which can result in more memory flipping in and out of the cache.
Declaring the array like Spellbound demonstrates is also
efficient if you want to iterate over the array for some
reason. You can set an int* to the first element and
then use the ++ operator each time you want to move
forward. If you want to move down one line, add width
to your pointer. This should make any operation that
needs to move over parts of the map (such as drawing a
visible portion of it the screen) much faster.
efficient if you want to iterate over the array for some
reason. You can set an int* to the first element and
then use the ++ operator each time you want to move
forward. If you want to move down one line, add width
to your pointer. This should make any operation that
needs to move over parts of the map (such as drawing a
visible portion of it the screen) much faster.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement