VarSetCapacity -1: updating string length v. capacity Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

VarSetCapacity -1: updating string length v. capacity  Topic is solved

20 Nov 2017, 21:37

I believe that using VarSetCapacity, and specifying -1, updates the perceived the length of the string, but doesn't alter the capacity of the variable. AFAICS, the documentation doesn't explicitly state that the capacity will remain the same.

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.
According to this test, the capacity does remain the same.

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
This is important in a situation where text is being repeatedly retrieved to the same buffer at a specific address, but where VarSetCapacity -1 is being used so that the text is ready to be appended to a growing variable or pushed into an array.

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Google [Bot], mikeyww, RussF and 287 guests