Skip to main content
gamedev.net
🔒 Locked

invalid redirection (c++)

Started by ekrax Jun 3, 2004 at 12:56 AM 2 replies 250+ views
Original Post
ekrax
ekrax
... i basically want to allocate space for a 2 dimensional array but it seems i can''t, this does not work int *p; int x = 10; int y = 10; p = new int [x][y]; of course i can just go p = new int [x * y] ... however in my case i later need to acces like p[8][4], or p[2][6] ... and multiply them together like p[8 * 4] obviosly doesnt work since that will be the same value as p[4][8] ... so can i not dynamically allocate 2 dimensional arrays? or am i doing something wrong? if i cant however, how can i solve this problem ... i have a map class which basically has all the variables and functions for operating a 2d tile map ... that variables that holds the data for each cells needs to be an array, but i do not want to give the array bounds since i want to be able to only allocate the exact space for how large each map i load is. so i need to be able to allocate space for the variable and then be able to access it similar to the fashion like map[1][2] would be 1 tile right and 2 down and map[2][3] would be 2 tiles .... etc, can i do this withough setting a predefined array boundries and without using the vector class ... maybe i should do this with linked lists? thanks for any help in advance.
Xai
Xai
I highly recommend you use the STL vector or deque classes if you can ...

here''s the C++ equivelent:


typedef std::vector<int> IntVector;
typedef std::vector<IntVector> IntVector2D;
typedef std::vector<IntVector2D> IntVector3D;

int x = 10;
int y = 10;

IntVector2D p(x); // allocate the outer dimention

for(int i=0; i<p.size(); ++i)
p[i].resize(y); // resize each inner list


// the above has created a list, which contains 10 lists, each of which contains 10 ints.

// and usage is VERY easy ...


p[3][2] = p[2][3]; // swaps two ints;

IntVector &currentList = p[3]; // currentList now points to the 4th set of 10 ints. While you can do this with arrays, using pointer arithmatic ... they don''t know their size / extent. Each vector in this setup, knows it''s size - so no need to constantly access global constants anymore.


quorn
quorn
boost.multi_map

Topic Locked

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

Sign in to reply to this topic.