Global Empty Array declaration

Just starting out? Need help? Post your questions and find answers here.
devox
User
User
Posts: 32
Joined: Thu Apr 01, 2021 7:25 pm

Global Empty Array declaration

Post 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.

Code: Select all

Global Dim items(-1)
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.

Code: Select all

Global Array items(1)
many thanks
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Global Empty Array declaration

Post 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,

:arrow: 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
:wink:
devox
User
User
Posts: 32
Joined: Thu Apr 01, 2021 7:25 pm

Re: Global Empty Array declaration

Post by devox »

Hi Marc56us,

I also considered going for that example, but the line

Code: Select all

Dim   A$(0)  : Debug ArraySize(A$())
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.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Global Empty Array declaration

Post 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)
devox
User
User
Posts: 32
Joined: Thu Apr 01, 2021 7:25 pm

Re: Global Empty Array declaration

Post by devox »

Thank you very much Olli, very interesting example. I will experiment with this.
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Global Empty Array declaration

Post by Fred »

This should work:

Code: Select all

Global Dim items(0)
FreeArray(items())
Debug ArraySize(items())
devox
User
User
Posts: 32
Joined: Thu Apr 01, 2021 7:25 pm

Re: Global Empty Array declaration

Post 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.
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Global Empty Array declaration

Post by Fred »

BTW, you will need to 'Dim' the array again after a FreeArray(), a 'ReDim' won't work.
Post Reply