Page 1 of 1
Global Empty Array declaration
Posted: Mon May 29, 2023 3:57 pm
by devox
Hi,
I was wondering if it is possible to declare an empty global array? I find when you declare an array you dimension it and therefore create the elements, but I would like to create an empty array and then dimension it at some later point at runtime. I suspect I can't do what I am thinking and the List is really the solution, but I thought I would ask none the less?
Here is an example of what I have tried but the compiler does not like. My first thought was to maybe dim the array using -1 to give a size of 0, but the compiler does not like it.
So I then thought maybe it uses a similar syntax to the array declaration of a parameter in a procedure. Which it does not like either.
many thanks
Re: Global Empty Array declaration
Posted: Mon May 29, 2023 4:26 pm
by Marc56us
devox wrote: Mon May 29, 2023 3:57 pm
I was wondering if it is possible to declare an empty global array? I find when you declare an array you dimension it and therefore create the elements, but I would like to create an empty array and then dimension it at some later point at runtime. I suspect I can't do what I am thinking and the List is really the solution, but I thought I would ask none the less?
Hi,

Help
Dim / Redim
Dim is used to create new arrays
ReDim is used to 'resize' an already declared array while preserving its content.
Example:
Code: Select all
Dim A$(0) : Debug ArraySize(A$()) ; 0
ReDim A$(15) : Debug ArraySize(A$()) ; 15

Re: Global Empty Array declaration
Posted: Mon May 29, 2023 5:15 pm
by devox
Hi Marc56us,
I also considered going for that example, but the line
Has one element dimensioned. The ArraySize() is a little misleadingly named as it actually returns the ubound of the array so therefore it's size is 1 and is not empty.
I was wondering if it is possible to create a declaration for it that is an array of zero elements? I have a feeling list is the real solution as from what I am reading you can only dim 1 or more elements.
Re: Global Empty Array declaration
Posted: Mon May 29, 2023 8:21 pm
by Olli
You could use a dynamical array, through a global pointor.
Code: Select all
Structure myArray
Array item.s(0)
EndStructure
Global *my.myArray = AllocateMemory(SizeOf(myArray) )
; (1) *my pointer points to an integer-sized buffer
InitializeStructure(*my, myArray)
; (2) create a one-cell-sized array (indexed with #0)
*my\item(0) = "12345"
Redim *my\item(7)
; (3) resize the array as an 8-cells-sized array
Debug *my\item(0)
; (4) redim function has not zeroed the initial value
Redim *my\item(0)
; (5) reduce the array to a single-cell size
ClearStructure(*my, myArray)
; (6) DESTROYS THE WHOLE ARRAY
; Debug *my\item(0)
; (7) triggers an error if uncommented
InitializeStructure(*my, myArray)
; (8) rebuild an array
Debug *my\item(0)
; (9) Displays 0 (but not the previewing '12345' value)
Re: Global Empty Array declaration
Posted: Mon May 29, 2023 8:36 pm
by devox
Thank you very much Olli, very interesting example. I will experiment with this.
Re: Global Empty Array declaration
Posted: Wed May 31, 2023 1:28 pm
by Fred
This should work:
Code: Select all
Global Dim items(0)
FreeArray(items())
Debug ArraySize(items())
Re: Global Empty Array declaration
Posted: Wed May 31, 2023 2:26 pm
by devox
@Fred
Thanks that is even better! Thank you very much. I wish you could init it with array size 0, rather than init with one element then clear, but it is a minor annoyance.
Re: Global Empty Array declaration
Posted: Wed May 31, 2023 3:01 pm
by Fred
BTW, you will need to 'Dim' the array again after a FreeArray(), a 'ReDim' won't work.