[SOLVED]Quick question: scope of STATIC variables in function Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

[SOLVED]Quick question: scope of STATIC variables in function

28 Oct 2019, 18:48

EDIT: Solved. Look at each posts that have been replied, they all expand and make it very clear. thanks

hello,
I'm not sure how to test if a static variable declaration in a function is going to overlap with a variable that has the same name somewhere else in the global / super global scope of the entire script. Also the documentation has left me wondering still on this specific question.

Please, do you know if this is by default going to be UNIQUE to this function alone in the entire script ?

Code: Select all

static isInitialized, patterns 
	
if (!isInitialized) {
	patterns := { "YYYY_MM_DD":regex["date", "YYYY_MM_DD"]
			  ,"YY_MM_DD":regex.date.YY_MM_DD
			  ,"YYYYMMDD":regex.date.YYYYMMDD
			  ,"YYMMDD":regex.date.YYMMDD }
	isInitialized := true
}
I just want to make sure isInitialized can be used without clashing with anything else in the script.

Thank you in advance for your help and time
Last edited by DRocks on 29 Oct 2019, 08:03, edited 1 time in total.
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: Quick question: scope of STATIC variables in function  Topic is solved

28 Oct 2019, 19:11

A static variable is also local. It will not clash with variables with the same name in other functions or outside of functions.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Quick question: scope of STATIC variables in function

28 Oct 2019, 19:12

Functions - Definition & Usage | AutoHotkey
https://www.autohotkey.com/docs/Functions.htm
Static variables are always implicitly local, but differ from locals because their values are remembered between calls.
So, if a variable is defined as static or local, it is local to the function. If you change a static/local variable's value, that only affects the static/local variable (within the function), and not the global variable with the same name.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Quick question: scope of STATIC variables in function

28 Oct 2019, 21:48

Okay. Thats very clear guys thanks to you both for the reply.

So how does this work?
A variable name is just a reference to another reference in the memory ?

I mean, isInitialized static local variable name can be used a thousand times but they all have a unique hwnd / id / memory adress?

So the only clash would be if by mistake I had a SUPER GLOBAL isInitialized somewhere?
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: Quick question: scope of STATIC variables in function

28 Oct 2019, 22:28

They each have their contents in a separate location in memory. You can see that by showing the memory address of each like this:

Code: Select all

x := 5
MsgBox, % &x
ShowLocal()
return

ShowLocal()
{
	local x := 3
	MsgBox, % &x
}
Even if you make x super-global by declaring it global outside of a function, it will still be considered local if you explicitly declare it local (or static) in your function. So you can't by mistake make it a super-global variable and mess up your function if you specifically declare it local in your function. This version of the script will show that the memory locations are still different:

Code: Select all

global x := 5
MsgBox, % &x
ShowLocal()
return

ShowLocal()
{
	local x := 3
	MsgBox, % &x
}
But if you take out the local declaration above, you'll see that they both point to the same address in memory.
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Quick question: scope of STATIC variables in function

29 Oct 2019, 02:49

Be careful comparing memory addresses; the address you get is not the address of the variable itself, but the address of the buffer which contains the variable's string value. (However, in AutoHotkey v2, you may get the address of a 64-bit number instead of a string.) All zero-capacity variables share the same address, because none of them have their own buffer.

It is sufficient to show that the variables have different values. If both were references to the same variable, they would obviously have the same value.

If you have a local variable in the current function and a global variable with the same name, ListVars will show them both separately.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Quick question: scope of STATIC variables in function

29 Oct 2019, 05:53

When you define a static variable: it is assigned a value at loadtime, it maintains its value between function calls. But the variable's *address* can change, as in the demo below, watch the number change every so often:

Code: Select all

Loop 100
	MyStaticVarFunc()
return

MyStaticVarFunc()
{
	static vText := ""
	MsgBox, % &vText "`r`n" vText
	vText .= "a"
}
If a string is to be appended to a variable, but the variable's string buffer is too small, the variable has to be changed to make more string space available.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: Quick question: scope of STATIC variables in function

29 Oct 2019, 06:19

lexikos wrote:
29 Oct 2019, 02:49
Be careful comparing memory addresses; the address you get is not the address of the variable itself, but the address of the buffer which contains the variable's string value. (However, in AutoHotkey v2, you may get the address of a 64-bit number instead of a string.) All zero-capacity variables share the same address, because none of them have their own buffer.

It is sufficient to show that the variables have different values. If both were references to the same variable, they would obviously have the same value.

If you have a local variable in the current function and a global variable with the same name, ListVars will show them both separately.
Good to know. Thanks. I used memory addresses instead of just comparing values to demonstrate the scope because OP asked specifically about memory locations in one of his questions.
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Quick question: scope of STATIC variables in function

29 Oct 2019, 08:01

Thank you very much for your help guys, this has all been clear and I get it now.

Interestingly, I never used ListVars before O.o
Most times I debug with msgboxes alone but ListVars would be faster lol :D thanks

Based on yours - here's another example for any one else looking at the post:

Code: Select all

#Persistent
global SUPER_GLOBAL_TEST_VAR := "BIG NAME"
MsgBox, % SUPER_GLOBAL_TEST_VAR
ShowLocal()
ListVars
return

ShowLocal()
{
	static SUPER_GLOBAL_TEST_VAR := "NOT THE SAME"
	MsgBox, % SUPER_GLOBAL_TEST_VAR
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, peter_ahk and 327 guests