Static scope or data initialization

New to FreeBASIC? Post your questions here.
AWPStar
Posts: 53
Joined: May 03, 2009 21:47

Static scope or data initialization

Post by AWPStar »

Hello.

I can put static variable at the start of procedure like that.

Code: Select all

sub test()
	static as long arr()
end sub
To initialize it i have to do something like that

Code: Select all

if ubound(arr)=-1 then redim arr(1023)
But is there a way do initialization at the frist proc run using fb syntax? Like...

Code: Select all

static scope
    redim arr(1023)
end scope
I know that static isn't the right word, but something similar.
SARG
Posts: 1875
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Static scope or data initialization

Post by SARG »

Hi,

You can use a 'flag' to do initialization when it's needed.

Code: Select all

sub test(init as boolean = false)
	static as long array()

	if init then
		redim array(1023)
	End If
	print ubound (array)
End Sub

test
test(1)
test
AWPStar
Posts: 53
Joined: May 03, 2009 21:47

Re: Static scope or data initialization

Post by AWPStar »

SARG
Sure, but that's not what i mean.
Now i use some variables to check if static variables initialized.
But what if fb has builtin syntax for that.
fxm
Moderator
Posts: 12536
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Static scope or data initialization

Post by fxm »

Another workaround:

Code: Select all

Sub test()
    Dim As Integer ub = 1023
    Static As Long array(ub)  '' pre-sized dynamic array
End Sub
or more compact:

Code: Select all

Sub test()
    Static As Long array(Type<Integer>(1023))  '' pre-sized dynamic array
End Sub
PeterHu
Posts: 222
Joined: Jul 24, 2022 4:57

Re: Static scope or data initialization

Post by PeterHu »

fxm wrote: Dec 13, 2024 10:02 Another workaround:

Code: Select all

Sub test()
    Dim As Integer ub = 1023
    Static As Long array(ub)  '' pre-sized dynamic array
End Sub
or more compact:

Code: Select all

Sub test()
    Static As Long array(Type<Integer>(1023))  '' pre-sized dynamic array
End Sub
I am sorry,what is the difference between

Code: Select all

Static As Long array(Type<Integer>(1023))
with

Code: Select all

Static As Long array(1023)
I just can't get it.
AWPStar
Posts: 53
Joined: May 03, 2009 21:47

Re: Static scope or data initialization

Post by AWPStar »

fxm
Very creative!

PeterHu
Static and Dynamic arrays.
I thinks it's about how you allocate memory.
fxm
Moderator
Posts: 12536
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Static scope or data initialization

Post by fxm »

If the values used with Dim to declare the dimensions of an array are all constants, the array will be created fixed length (of Static size, unless Option Dynamic is specified), while using one or more variables to declare the dimensions of an array makes it variable length, even if Option Static is in effect.

- Static fixed-length array:
Static As Long array(1023)
('1023' is a constant)

- Static variable-length array:
Static As Long array(Type<Integer>(1023))
('Type<Integer>(1023)' is a temporary variable)
deltarho[1859]
Posts: 4675
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Static scope or data initialization

Post by deltarho[1859] »

How is 'Type<Integer>(1023)' a temporary variable?
fxm
Moderator
Posts: 12536
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Static scope or data initialization

Post by fxm »

See documentation:
Type (Temporary)
deltarho[1859]
Posts: 4675
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Static scope or data initialization

Post by deltarho[1859] »

I read that before my post but 'Integer' is not a UDT so we are stretching it a bit.

If I buy that how do we get a variable length array and use 1023?

PeterHu wrote "I just don't get it."

Niether do I.
adeyblue
Posts: 350
Joined: Nov 07, 2019 20:08

Re: Static scope or data initialization

Post by adeyblue »

Would it be similarly mysterious if it was instead

Code: Select all

Function ArraySize() As Long
    Return 1023
End Function

Static As Long array(ArraySize())
(I don't know if this works, it's just for illustration)

Same deal, just that Type<> is a built-in language thing rather than a function you write yourself.
fxm
Moderator
Posts: 12536
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Static scope or data initialization

Post by fxm »

In addition to UDTs, 'Type (temporary)' also supports all numeric types (including pointers) and the var-len string type (fix-len string types are not supported).

This is already stated in the documentation (Type (Temporary)):
It can create not only a temporary copy of an user defined type, but also a temporary copy of predefined data-type as a variable-length string or any numeric data-type (all standard data-types excluding fixed-length strings).

In the particular case where a temporary type is used in a 'With' expression, the destruction of the temporary type is deferred to the end of the 'With' scope.
(sentence now added in the Type (Temporary) documentation)
deltarho[1859]
Posts: 4675
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Static scope or data initialization

Post by deltarho[1859] »

Thanks fxm. We are no longer "stretching it a bit."

The 'With' example is not a goof example. Who in their right mind would use it for 'ex'. If 'ex' was a longer name, then the longer it was, the more sense it would be to use 'With'.
dodicat
Posts: 8231
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Static scope or data initialization

Post by dodicat »

Crude dynamic tester.

Code: Select all

 
#cmdline "-exx"
type udt
    as long a(20)
end type


function isdynamic overload(a() as long) as boolean
    on local error goto fin
var k=abs(ubound(a))
redim a(2*k)
return iif(ubound(a)=k,false,true)
fin:
end function

function isdynamic overload(a() as udt) as boolean
    on local error goto fin
var k=abs(ubound(a))
redim a(2*k)
return iif(ubound(a)=k,false,true)
fin:
end function



redim as long arr(23)
print isdynamic(arr())

dim as long arr2(23)
print isdynamic(arr2())

static as long arr3(12)
print isdynamic(arr3())

static as long arr4(type<integer>12)
print isdynamic(arr4())


print
static as udt z(rnd*12)
print isdynamic(z())
static as udt z2()
print isdynamic(z2())
redim z2(1 to 5)
print isdynamic(z2())

dim as udt z3(-5 to -1)
print isdynamic(z3())
 
sleep
 
fxm
Moderator
Posts: 12536
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Static scope or data initialization

Post by fxm »

deltarho[1859] wrote: Dec 14, 2024 12:48 The 'With' example is not a goof example. Who in their right mind would use it for 'ex'. If 'ex' was a longer name, then the longer it was, the more sense it would be to use 'With'.
Yes, I increased the length of the instance name.
Post Reply