AutoHotkey Community

It is currently May 26th, 2012, 7:13 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: May 30th, 2009, 4:20 am 
Offline

Joined: November 27th, 2008, 9:44 am
Posts: 62
See the code examples. The three functions ostensibly all do the same thing, but func3 ends up yielding a different result. I don't understand why. When I compared the the return value before and after returning, they were the same. Why does it freak out about the zeroth offset?


Code:
func1(a, 1234, 5678)
msgbox % NumGet(a, 0) " : " NumGet(a, 4) ; displays "1234 : 5678"

func2(1234, 5678)
msgbox % NumGet(n, 0) " : " NumGet(n, 4) ; displays "1234 : 5678"

b := func3(1234, 5678)
msgbox % NumGet(b+0, 0) " : " NumGet(b+0, 4) ; displays "1024 : 5678"

func1(ByRef n, x=0, y=0)
{
   VarSetCapacity(n, 8)
   NumPut(x, n, 0)
   NumPut(y, n, 4)
}

func2(x=0, y=0)
{
   global n
   VarSetCapacity(n, 8)
   NumPut(x, n, 0)
   NumPut(y, n, 4)
}

func3(x=0, y=0)
{
   VarSetCapacity(n, 8)
   NumPut(x, n, 0)
   NumPut(y, n, 4)
   return &n
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 4:37 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Unless you use Static n / Global n in Func3() the variable content is lost when the function returns.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 4:58 am 
Offline

Joined: November 27th, 2008, 9:44 am
Posts: 62
SKAN wrote:
Unless you use Static n / Global n in Func3() the variable content is lost when the function returns.


Then how does it get the right content at offset 4, and "kind of close" with the offset at 0:

Code:
b := func3(1234, 5678)
msgbox % NumGet(b+0, 0) " : " NumGet(b+0, 4) ; displays "1024 : 5678"

b := func3(2345, 5678)
msgbox % NumGet(b+0, 0) " : " NumGet(b+0, 4) ; displays "2304 : 5678"

b := func3(3456, 5678)
msgbox % NumGet(b+0, 0) " : " NumGet(b+0, 4) ; displays "3328 : 5678"

b := func3(4567, 5678)
msgbox % NumGet(b+0, 0) " : " NumGet(b+0, 4) ; displays "4352 : 5678"


func3(x=0, y=0)
{
   VarSetCapacity(n, 8)
   NumPut(x, n, 0)
   NumPut(y, n, 4)
   return &n
}


The behavior just seems really weird to me.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 5:09 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
rulfzid wrote:
Then how does it get the right content at offset 4, and "kind of close" with the offset at 0:


The first byte is overwritten with a Null and here is the proof:

Code:
mAdd := Func()

Loop 7
 MsgBox, 0, % "Offset:" A_Index-1, % DllCall("MulDiv", Int,mAdd+A_Index-1, Int,1, Int,1, Str )

Func() {
 Var := "rulfzid"
 Return &Var
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 3:20 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
That behaviour is undocumented and should in no way be relied upon. Additionally, it only happens if the variable was allocated "persistent" memory (< 64 bytes). The important point is that it is not logical or advisable to use a local variable's address after the function returns.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 31st, 2009, 12:11 am 
Offline

Joined: November 27th, 2008, 9:44 am
Posts: 62
@SKAN & @ Lexikos

Thanks for the insights. I think I've figured out how to get around what I want - I'm using the winapi heap memory functions.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], bobbysoon, BrandonHotkey, Cephei1, Miguel, rbrtryn, Xx7 and 70 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