Jump to content
  • Advertisement

Archived

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

Burning_Ice

problem with arrays

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

Hi How can I specify the size of an array at runtime?? I want try to do somethink like this: void myfunc(int x,int y) { int buff[x][y]; } void main(void) { myfunc(10,20); } but this always gives me an compiler error ''constant expression expected'' so isn''t there a way to specify an array at runtime? Any help will be appreciated! Burning_ice

Share this post


Link to post
Share on other sites
Advertisement
That example will (even if it doesn''t work) only create a array in the myfunc function. It will disappear when the function ends.

You could use linked lists, but I don''t know what you are going to use the array for, so I can''t help you more than that.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A) Write a template for an array class (or use STL).

B) Dynamically allocate memory and access directly. For 2-dimensional arrays you will need to use a macro to access it.

e.g. 1 dimension

Int *pnArray = new Int[NUM_YOUR_ELEMENTS];
pnArray[0] = 1;

or 2 dimensions

#define ACCESS_ARRAY(x,y) (pnArray[x + y * XDIM])

Int *pnArray = new Int[XDIM * YDIM];
ACCESS_ARRAY(1,2) = 5;

and at end don''t forget

delete [] pnArray;


Share this post


Link to post
Share on other sites
If you want to dynamically allocate but not have to use a macro to access, you can still do it, though it is sort of tedious:

int **Create2DArray (int sizeX, int sizeY) {

int **ary;
int i;

ary = (int**) malloc (sizeof (int *) * sizeX);
for (i = 0; i < sizeX; i++) {
ary = (int *) malloc (sizeof (int) * sizeY);
}

return ary;
}

Of course, this is slower than it has to be and fairly annoying to free so this is the better method:

int **Create2DArray (int sizeX, int sizeY) {

int **ary;
int i;
int size;

size = sizeof (int *) * sizeX + sizeof (int) * sizeY * sizeX;

ary = (int**) malloc (size);
for (i = 0; i < sizeX; i++) {
ary[i] = (int *) (ary + sizeof (int*) * sizeX + sizeof (int) * sizeY * i;
}

return ary;
}


The second may look a little gnarly at first glance, but its fairly elegant (in the c style, so no c++ flames please!) It cuts your malloc and free needs to 1 of each instead of sizex+1, and you can address it like a "normal" 2d array.

Disclaimer: I didn''t check or compile the code, so don''t use it blindly!

Notwen!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I would create a template class and then overload the
[] operator. Out there somewhere is an apmatrics class that
can handle 2 dimensional arrays like this. There is also
an apvector class that does the same for one dimensional arrays. You''ll have to search for them though.

Miracle Man

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!