cant seem to create a dynamic array C++

Started by
6 comments, last by Zahlman 18 years, 11 months ago
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?
These tears..leave scars...as they run down my face.
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 herefor(int i=0;i<NUMOFCHUNKS){	delete[] collisionTracker;}delete[] collisionTracker;
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**.
"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
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.
These tears..leave scars...as they run down my face.
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];
?
These tears..leave scars...as they run down my face.
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.
cool the "pretend" 2D array sounds like the easiest to implement Ill try that.

Thanks
These tears..leave scars...as they run down my face.
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.

This topic is closed to new replies.

Advertisement