STATIC Variable Initialisation

Previous topic - Next topic

Thunor

Your help example :-
Code (glbasic) Select
// STATIC / LOCAL
FOR i=0 TO 5
   foo()
NEXT
SHOWSCREEN
MOUSEWAIT

FUNCTION foo:
LOCAL  l = 5
STATIC s = 5
   INC l, 1
   INC s, 1
   PRINT "l="+l+" s="+s, 0, s*20
ENDFUNCTION
...always prints "i=6 s=6" and that is all! This means that s is being reinitialised every time the function is executed which is wrong. It must be initialised the first time it is executed only.

Interestingly if I don't initialise s i.e. STATIC s it prints :-

i=6 s=1
i=6 s=2
i=6 s=3
i=6 s=4
i=6 s=5
i=6 s=6

... but it should be printing :-

i=6 s=6
i=6 s=7
i=6 s=8
i=6 s=9
i=6 s=10
i=6 s=11

Regards

D2O

Code (glbasic) Select
// STATIC / LOCAL
FOR i=0 TO 5
   foo()
NEXT
SHOWSCREEN
MOUSEWAIT

FUNCTION foo:
LOCAL  l = 5
STATIC s
STATIC s_CTRL
IF s_CTRL = 0
s = 5
s_CTRL = 1
ENDIF  


   INC l, 1
   INC s, 1
   PRINT "l="+l+" s="+s, 0, s*20
ENDFUNCTION
I7 2600K; 8 GB RAM ; Win10 Pro x64 | NVidia GTX 750 TI 2048MB ; Realtec OnBoard Sound;
Lenovo ThinkPad T400: XP Pro
GLB Premium-immer Aktuell

Thunor

Thanks, an effective solution but I won't want the additional overhead of doing that for every static variable throughout my project. It would be easier to use global variables which defeats the point.

I've had a look in the cpp code generated by GLBasic for my project and I can see why my static variable is being reinitialised every time the function is called.

This GLBasic line :-
Code (glbasic) Select
STATIC drag_id = UNDEFINED...is being translated to cpp as :-
Code (glbasic) Select
static REGISTER_VAR_DEF(DGInt, drag_id,  UNDEFINED);
drag_id = UNDEFINED;
UNDEFINED is a global variable and equals -1. The second line drag_id = UNDEFINED; is the problem. In fact any initialised variable will look similar to the above even though REGISTER_VAR_DEF is being passed an initial value.

Kitty Hello


Thunor

Quote from: GernotFrischOh no! Huge bug!
Hehe, sorry :)

I've decided to use D²O's method above for the time being, or something similar. One CTRL flag could be used to initialise many static variables so it's not such an overhead as I originally thought.

AndyH

Hey, I've found another odd thing with STATIC.  Try this code:

Code (glbasic) Select
LOCAL i
FOR i=0 TO 10
LOCAL tt = test()
PRINT tt,0,i*8
NEXT

SHOWSCREEN
MOUSEWAIT


FUNCTION test:
STATIC _xx
INC _XX,1
RETURN _XX
ENDFUNCTION
It should print out numbers in steps of 1, but it steps in twos.  OUTPUT = 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22

The offending line is:
Code (glbasic) Select
LOCAL tt = test()Change this to
Code (glbasic) Select
LOCAL tt
tt = test()
And it all works.  Output is now (as expected) = 1,2,3,4,5,6,7,8,9,10,11

Is this related or a separate bug?

Thunor

The real issue is not static or local but variable initialisation.

GLBasic converts glbas to cpp like so :-
Code (glbasic) Select
LOCAL tt = test()

...results in...

REGISTER_VAR_DEF(DGInt, tt,  test());
tt = test();
...so I think you can see why it's incrementing in 2s. By the time you print tt, test() has been executed twice.

The reason it works if you seperate LOCAL tt is this :-
Code (glbasic) Select
LOCAL tt
tt = test()

...results in...

REGISTER_VAR_DEF(DGInt, tt, 0);
tt = test();
The same thing happens with static variable initialisation although the consequences are different.

You can simply refrain from using LOCAL in this way to prevent this problem from occuring.

BTW look in C:\Documents and Settings\user\Local Settings\Temp\glbasic\ to view your converted source code.

Kitty Hello

oughta be fixed in new update.