Skip to main content
GameDev.net gamedev.net
🔒 Locked

Array/Malloc question

Started by utwo007 Apr 22, 2002 at 3:50 PM 6 replies 1.2k views
Original Post
utwo007
utwo007
Can I declare an array like this:
int myArray[100];
 
And then access the 30th element like this?:
myArray[3][10]
 
Also, can I do something like this:
int * myArray;
myArray = malloc(sizeof(int)*100);
 
And then access the 30th int like this?:
myArray[3][10];
 
The reason I ask is because I want to create a simply game with a map made of tiles. I will create a tile object and then create a 2D array of these tile objects to represent the map. However, different levels in the game will require different map sizes, so I want to be able to allocate the memory for the map currently in play dynamically, but also have the benifit of the 2D array. Would the following work? int * myArray = new int[x][y]; ---signature--- People get ready. I''m ready to play.
---signature---People get ready.I'm ready to play.
Fruny
Fruny
No.

Multidimensional arrays (MD arrays for short) can only be accessed in a MD fashion if they have been declared as MD arrays in the first place. Otherwise, the compiler has no way of knowing how large is each dimension.

You could create the MD array manually :

  
int** array;
array = new int*[x];
for( i=0; i<x; ++i )
array[i] = new int[y];

array[5][5] = 3;


Then when deleting the array :

  
for( i=0; i<x;++i )
delete[] array[i];
delete[] array;


Or, (much better), use a vector of vectors;

  
std::vector<vector<int> > array;
array.resize(x);
for(i=0; i<array.size(); ++i )
array[i].resize(y);

array[5][5] = 3;

The vector is an auto variable which doesn''t need to be deleted.



[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
sjelkjd
sjelkjd
To expand on Fruny's post...
A more concise way to construct these vectors is as follows:

  
vector<int> base(y,0); //makes a vector y big, initialized with zeros

vector<vector<int> > bigvec(x,base); //makes a vector of vector<int>, all initialized with base

//now we have an x by y matrix

bigvec[5][5]=3;

Or if you wanna be really hardcore

  
vector<vector<int> > bigvec(x,vector<int>(y,0));

edit: html ate my < > s

[edited by - sjelkjd on April 22, 2002 5:36:43 PM]
eldee
eldee
quote:
Original post by utwo007
Can I declare an array like this:


int myArray[100];


And then access the 30th element like this?:


myArray[3][10]




what you''re trying to do can''t be accomplished, due to the
fact taht you''re trying to access a single dimensioned
array in a multidimensional way. what you can do instead
of

myArray[3][10] to access the the 30th element, you can do this:
myArray[3*10] <= 3*10 being 30.. giving you the 30th element.


-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

utwo007
utwo007
quote:
Original post by eldee
what you can do instead
of

myArray[3][10] to access the the 30th element, you can do this:
myArray[3*10] <= 3*10 being 30.. giving you the 30th element.





Very nice. I think I''ll do it this way.



---signature---
People get ready.
I''m ready to play.
---signature---People get ready.I'm ready to play.
Fruny
Fruny
quote:
Original post by sjelkjd
To expand on Fruny''s post...



/me should have remembered that.

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Beer Hunter
Beer Hunter
Here''s another way to access a one-dimensional array as if it were a two-dimensional array (untested):

  
int *one_dimensional_array = new int[width*height];
int **two_dimensional_array = new (int*)[height];
for(int i = 0; i < height; ++i) {
two_dimensional_array[i] = &(one_dimensional_array[width * i]);
}
...
two_dimensional_array[row][column] = 500;
// same as one_dimensional_array[row * width + column] = 500;

...
delete[] two_dimensional_array;
delete[] one_dimensional_array;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.