IonicWind Software

IWBasic => General Questions => Topic started by: Andy on October 14, 2015, 02:43:48 AM

Title: Silly question time again - setting array sizes
Post by: Andy on October 14, 2015, 02:43:48 AM
Hi,

I was wondering why I cannot use an INT to set an array size.

If I use:
STRING MyString[1000] - that's okay - no problem.

But what if I don't want to use up memory, and I don't yet know how big to make the array?

E.g. If I can count up something, and know the size I need is say 876, why do I get a compile error when I try this....


1. INT CountUp = (say 876)
2. STRING MyString[CountUP]

I get "duplicate definition of label or variable: CountUp" error.

Was just wondering about this, and can it be done?

Thanks,
Andy.




Title: Re: Silly question time again - setting array sizes
Post by: Brian on October 14, 2015, 03:00:59 AM
Andy,

There is a workaround for this - I remember Paul Turley telling me once. I would have to look
at my myriad bits of code at home to find it, though. Some Basics have a REDIM command,
where you set an initial value, do a count of your total records, and then REDIM your initial
command. Would be nice in IWB, I reckon

Brian
Title: Re: Silly question time again - setting array sizes
Post by: LarryMc on October 14, 2015, 07:11:32 AM
when you declare variables, regardless of where they are located in your program they are processed during the compile time and not during run time.
Taking your two lines of code I'll describe what happened during compile time:
INT CountUp = 876
allocates memory for a variable named CountUp and would normally initialize it to 0 or not based on a flag in Compiler Options but in this case because of "=876" it is initialized to 876. Since the program has yet to me compiled the value is not used for anything at this point.

STRING MyString[CountUP]
the compiler recognizes this as a string array so it looks in the brackets for an INT value for the number of elements in the array.  The compiler, when it finds a variable where a INT value is expected, attempts to DEFINE the variable (just like INT CountUp) and that is where the duplicate variable error comes from.
FYI - this automatic defining of variable types by the compiler can be controlled by the user with the AUTODEFINE  on/off command but not in this case

Now, this will work
CONST CountUp =  876
STRING MyString[CountUP]

Title: Re: Silly question time again - setting array sizes
Post by: Bruce Peaslee on October 14, 2015, 09:29:13 AM
I do not believe arrays can be re-dimensioned. If I need to store an indeterminate amount of data, I use linked lists.
Title: Re: Silly question time again - setting array sizes
Post by: ckoehn on October 14, 2015, 11:31:19 AM
This is a post from Paul..

Quotepointer p
p = new(INT, size)
#p[index] = value
print #p[index]
delete p

To change the size of the array you have two choices.  Use GlobalRealloc from the Windows API or allocate a second array the new size and copy the old contents.

Later,
Clint
Title: Re: Silly question time again - setting array sizes
Post by: Andy on October 15, 2015, 12:23:32 AM
Thanks everyone for your posts, I will have a play around with the suggestions.

Andy.
:)
Title: Re: Silly question time again - setting array sizes
Post by: LarryMc on October 15, 2015, 10:16:34 AM
I'm 3/4 of the way  through a long tutorial/example that I'll try to get posted later today.
Don't give up on me.