Jump to content
  • Advertisement
Sign in to follow this  
donjonson

cant seem to create a dynamic array C++

This topic is 4912 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 am trying to creat a dynamic array using the following line
int* collisionTracker = new int[NUMOFCHUNKS][ NUMOFCHUNKS];

I am getting the folowing compiller error

.\gravSim.cpp(27) : error C2540: non-constant expression as array bound

any ideas what i am doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
You have to do it like this
int** collisionTracker = new int*[NUMOFCHUNKS];
for(int i=0;i<NUMOFCHUNKS){
collisionTracker = new int[NUMOFCHUNKS];
}

// use collisionTracker here

for(int i=0;i<NUMOFCHUNKS){
delete[] collisionTracker;
}
delete[] collisionTracker;


Share this post


Link to post
Share on other sites
Quote:
any ideas what i am doing wrong?


Only the outermost dimension can be a variable, is the only dimension that can be passed to array new. You either need to give up the 2D-ness or the ability to dynamically specify the second dimension.

int* foo = new int[n*n];

Or
typedef int foo[20];
foo* array = new foo[n];


Note that foo* is NOT an int*, nor an int**.

Share this post


Link to post
Share on other sites
hmm could i create a 2 dimentional vector for such a purpose?

I want to be able to access any member of the array by just using
say

collisionTracker[4][7] = 5;

is this possible with vectors or some other tool?

I cant really give up the 2D-ness or the ability to dynamically specify the second dimension.

Share this post


Link to post
Share on other sites
I guess I could just make the first dimension HUGE so I dont have to worry about it beeing to small but that seems like a waste.


if I did the following

int i;
int ** cpp;
collisionTracker = new int*[NUMOFCHUNKS];
for(i = 0; i < NUMOFCHUNKS; i++)
collisonTracker = new int[NUMOFCHUNKS];




could i access data in the array by using

collisionTracker[70][4];
?

Share this post


Link to post
Share on other sites
Along the lines of what Fruny said, you can make it 1d but pretend it's 2d.

So instead of:
int** array = new int[width][height];

you write:
int* array = new int[width*height];

and when accessing, instead of writing:
array[x][y]

you write:
array[x*width + y]

And you can make that code prettier by writing an accessor or something, like getArrayElement(x,y).

And a different solution- the Boost library has a multidimensional array template class, if you are into templates.

Share this post


Link to post
Share on other sites
Quote:
Original post by donjonson
cool the "pretend" 2D array sounds like the easiest to implement Ill try that.

Thanks


This is what Fruny meant with giving up the 2D-ness - you no longer get the syntax you'd like, but it's always possible to treat a 1D array as 2D, by simply doing manually what the compiler would normally do for you (since true "2D arrays" will be stored in a single chunk of memory that basically gets addressed like a 1D array).

Mike's way (which you reprised, minus the deletion code - do remember that your deletes need to exactly balance your news) will give you the syntax you want, at the expense of some runtime overhead. It will also have different semantics: the array is no longer necessarily "rectangular" - you are responsible for keeping it so. That is, in your inner loop to allocate each "line", there's nothing requiring you to keep NUMOFCHUNKS (the "line length") constant. Each is handled as a separate array, which is pointed at from the original int**. Depending on your application, this could be good, bad, or mostly irrelevant.

You should also see here.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!