Jump to content

  • Log In with Google      Sign In   
  • Create Account


problem with arrays


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Burning_Ice   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 January 2000 - 06:09 AM

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

Sponsor:

#2 ZomeonE   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 January 2000 - 06:15 AM

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.

#3 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 07 January 2000 - 06:28 AM

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;




#4 Notwen   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 January 2000 - 08:07 AM

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!



#5 Burning_Ice   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 January 2000 - 08:18 AM

Thank you all!
now it works!

#6 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 07 January 2000 - 08:40 AM

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

#7 Yanroy   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 January 2000 - 10:09 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS