Array sizes
3 messages in this thread
Thanks for the advice. I did get a chance to talk to Lattice about the problem
and they said it was a bug fin 5.02 that arrays could not be over 32k without
increasing your stack size. They said this is fixed in 5.04.
if I did allocate the memory usng malloc, how would you index the array. Does
the pointer increment by integer size. How about an array of structure, how
would you control the pointer here.
thanks for the help.
scott-
Scott,
When you allocate memory for an array with malloc(), you cast the pointer
which malloc() returns for you to the type that you require. For example,
#define ARRAY_SIZE 1000L
int *i_ptr;
struct foobar *foo; /*defined somewhere else*/
i_ptr = (* int)malloc(ARRAY_SIZE * sizeof(int));
foo = (* struct foobar)malloc(ARRAY_SIZE * sizeof(struct foobar));
Under this example, i_ptr would be a pointer to an integer at the beginning of
a block of memory long enough to hold 1000 integers. You can then treat this
block as an array of 1000 integers. The value of the first element can be
addressed as *i_ptr or as i_ptr[0]; the second element as *(i_ptr + 1) or as
i_ptr[1]; and the last element as *(i_ptr + 999) or as i_ptr[999]. Variable
foo would be a pointer to a foobar structure at the beginning of a block of
memory long enough to hold 1000 such structures. You can treat this block as an
array of 1000 foobar structures with the contents of the first element
addressed as foo[0] or as *foo, the second element as *(foo + 1) or foo[1],
etc. The compiler will scale the pointer according to the size of the data
type the pointer points to. That's why you have to cast the return value from
malloc() to the appropriate pointer type.
BTW, you can use any integer type for the array subscripts, so if your
array has more than 32K elements, you need to use a 32 bit integer.
Darron
Darron, Thank you very much for your detailed reply. I'm learning!
-Scott