problem with arrays

Started by
5 comments, last by Burning_Ice 24 years, 3 months ago
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
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.
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;


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 = (int *) (ary + sizeof (int*) * sizeX + sizeof (int) * sizeY * i;<br> }<br><br> return ary;<br>}<br><br><br>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.<br><br>Disclaimer: I didn''t check or compile the code, so don''t use it blindly!<br><br>Notwen!<br><br> </i>
Notwenwww.xbox.com
Thank you all!
now it works!
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
On my compiler, the origional code works! I can''t explain it. I was kind of puzzled by this discussion at first.

www.trak.to/rdp

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

This topic is closed to new replies.

Advertisement