VarSetCapacity()
https://autohotkey.com/docs/commands/VarSetCapacity.htm
Code: Select all
In v1.0.44.03+, specify -1 for RequestedCapacity to update the variable's internally-stored string length to the length of its current contents.
Code: Select all
q:: ;test VarSetCapacity
vText := ""
VarSetCapacity(vText, 100000)
MsgBox, % VarSetCapacity(vText, -1) " " VarSetCapacity(vText) " " &vText "`r`n" vText
vText := "abcdefghijklmnopqrstuvwxyz"
MsgBox, % VarSetCapacity(vText, -1) " " VarSetCapacity(vText) " " &vText "`r`n" vText
NumPut(0, &vText, 10, "UShort")
MsgBox, % VarSetCapacity(vText, -1) " " VarSetCapacity(vText) " " &vText "`r`n" vText
NumPut(Ord("f"), &vText, 10, "UShort")
MsgBox, % VarSetCapacity(vText, -1) " " VarSetCapacity(vText) " " &vText "`r`n" vText
return
Anyway, I believe I've answered my own question, but I'll leave it here in case it can be interesting to others. Thanks for reading.
[EDIT:] In the example above, the address stayed constant. If the variable had been decreasing in size each time we did VarSetCapacity -1, then there might not be enough space for the next string due to be placed there.
An example of the address shifting as the variable size is increased.
Code: Select all
w:: ;test VarSetCapacity: resize variable
VarSetCapacity(vText, 10000)
MsgBox, % &vText
VarSetCapacity(vText, 5000)
MsgBox, % &vText
VarSetCapacity(vText, 50000)
MsgBox, % &vText
VarSetCapacity(vText, 100000)
MsgBox, % &vText
return