Jump to content
  • Advertisement


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


invalid redirection (c++)

This topic is 5283 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

... 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.

Share this post

Link to post
Share on other sites
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 ¤tList = 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.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!