AutoHotkey Community

It is currently May 26th, 2012, 9:03 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: VarSetCapacity issue
PostPosted: July 19th, 2005, 12:05 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The first MsgBox in the following script shows an invisible character for x, length 1. The second MsgBox shows the correct ! character surrounded by [ and ].
Code:
x = ?
GrantedCapacity := VarSetCapacity(x, 256, 0)
DllCall("RtlFillMemory", "UInt", &x, "UInt", 1, "UChar", 33)
MsgBox % x . "`n" . StrLen(x)
MsgBox % "[" . x . "]" . "`n" . StrLen(x)
Removing the VarSetCapacity line cures the problem.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 19th, 2005, 1:51 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
This is caused by the fact that the program internally maintains the length of each variable's contents. But when you alter the contents by passing the address to a function, the program has no way of knowing what happened and thus the length is now inaccurate, which in turn causes misbehavior.

I'm not sure if this can be easily fixed, but I'll look into it more. Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 19th, 2005, 11:28 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
I forgot to mention that you can work around this in most cases by passing the variable as a "str" argument and omitting the ampersand prefix. By passing it as a string, the program knows that the length might have changed and so updates it after the call:

DllCall("RtlFillMemory", Str, x, UInt, 1, UChar, 33)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 19th, 2005, 3:20 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Thanks, Chris! "Str" cures the problem.

I wonder, if you store the length of the strings, why do you need the terminating \0 ? Without it binary data could be handled more easily.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 20th, 2005, 2:37 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Laszlo wrote:
if you store the length of the strings, why do you need the terminating \0 ?
Since all the C library functions require terminated strings, it simplifies a lot of things to have it that way. A lot of code would have to be rewritten (and some new code added) to be able to omit the terminators.

The length-tracking is done because it improves performance by avoiding CPU-consuming calls to strlen() [which calculates the length of a string].

Update: In v1.0.44.03, VarSetCapacity() has been improved to interpret a capacity of -1 as "update this variable's internally-stored length". This is useful in cases where a variable has been altered indirectly, such as by passing its address via DllCall().


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 5 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group