AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

my function is not returning expected results

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Static



Joined: 18 Dec 2008
Posts: 54

PostPosted: Wed Jun 10, 2009 7:18 pm    Post subject: my function is not returning expected results Reply with quote

Code:
SetFormat integer,hex
LeftChild := "This is the left Child"
RightChild := "This is the right Child"
ParentString := "this string will be stored in the parent node"

Parent := MakeNode(&LeftChild,&RightChild,ParentString)
blah := StrLen(parent)
msgbox %Blah%

shouldbeleft:= NumGet(parent,0)
shouldberight:= NumGet(parent,4)
shouldbelen:= NumGet(parent,8)
msgbox This is what the parent variable looks like RAW:`r%parent%`rLeft:%left%  Right:%right%  Value:%value%  Len: %len%`rShouldbeLeft: %shouldbeleft%  Shouldberight: %shouldberight%  ShouldbeLen: %shouldbelen%





MakeNode(Left,Right,value) {
len := StrLen(value)
temp2:=""
VarSetCapacity(temp2,len+16, 0)
NumPut(Left+0,temp2)
NumPut(Right+0,temp2,4)
NumPut(len+0,temp2,8)
NumPut(value,temp2,12)

blah := StrLen(temp2)
msgbox %Blah%

shouldbeleft:= NumGet(temp2,0)
shouldberight:= NumGet(temp2,4)
shouldbelen:= NumGet(temp2,8)
msgbox This is what the temp2 variable looks like RAW:`r%temp2%`rLeft:%left%  Right:%right%  Value:%value%  Len: %len%`rShouldbeLeft: %shouldbeleft%  Shouldberight: %shouldberight%  ShouldbeLen: %shouldbelen%

return temp2
}


The message box in the function displays the correct values, but i try to do the same thing outside the function and its coming up empty. Not sure what im doing wrong. Thanks
Back to top
View user's profile Send private message
Static



Joined: 18 Dec 2008
Posts: 54

PostPosted: Wed Jun 10, 2009 11:52 pm    Post subject: Reply with quote

I changed the function to MakeNode(Left,Right,value, ByRef temp2) and that worked, though I don't understand why it doesn't work as a return value. If any one can explain why it would satisfy my curiosity Smile
Back to top
View user's profile Send private message
JDN



Joined: 24 Mar 2004
Posts: 299

PostPosted: Thu Jun 11, 2009 2:23 am    Post subject: Reply with quote

Are you familiar with AHK's rules for global variables?

You use the variable Len inside the function and outside the function. But these are two separate and different variables.

Since you do not define the variable inside the function as "Global", it is a local variable when used in the function and a global variable when used outside the function.

I'm not sure if this is your problem or not. But if you don't know the rules concerning global variables, you really should read the section in the Help file on "Functions" and read about Global and Static and ByRef. They are all very important concepts.
Back to top
View user's profile Send private message
Static



Joined: 18 Dec 2008
Posts: 54

PostPosted: Fri Jun 12, 2009 5:02 am    Post subject: Reply with quote

I definitely understand variable scopes or I never would have tried the byref idea.

It seems to me that, if inside of a function I execute a "return x" statement, and then in a function call do y:= foo() , then y should equal x.

Can anyone just paste this code into an autohotkey script and run it and verify that parent is not equaling temp2 from the function? I've tried on two PCs and get the same results. I even tried using dereference operator on the return statement thinking maybe using varsetcapcity on temp2 forced it into a pointer or something. There has to be something simple i'm missing here.

You should get the following results (addresses of course wont be the same) (the only parts we care about are the shouldbe variables)

*** First MessageBox from inside function
This is what the temp2 variable looks like RAW:

Left:0x1d22e60 Right:0x1d22ea0 Value:this string will be stored in the parent node Len: 0x2d
ShouldbeLeft: 0x1d22e60 Shouldberight: 0x1d22ea0 ShouldbeLen: 0x2d

*** Second MessageBox in auto exec section, after function call
This is what the parent variable looks like RAW:

Left: Right: Value: Len:
ShouldbeLeft: Shouldberight: ShouldbeLen:

In fact, lets simplify the problem, and try the code below. same thing happening.


Code:
SetFormat integer,hex
LeftChild := "This is the left Child"

Parent := MakeNode(&LeftChild)
shouldbeleft:= NumGet(parent,0)

msgbox main(): %shouldbeleft% (address of LeftChild)

MakeNode(Left) {
VarSetCapacity(temp2,4, 0)
NumPut(Left+0,temp2)
shouldbeleft:= NumGet(temp2,0)

msgbox infunction: %shouldbeleft% (address of LeftChild)

return temp2
}
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7293
Location: Australia

PostPosted: Fri Jun 12, 2009 8:50 am    Post subject: Reply with quote

Functions can only return strings; they cannot handle binary values. Specifically, return temp2 will only return the portion of temp2 up to the first zero-byte. You could instead return the address of the variable (&temp2), but since it is a non-static local variable it will not be valid after the function returns. One way to work around this is to manually allocate memory:
Code:
Parent := MakeNode(&LeftChild)
msgbox % "main(): " . NumGet(Parent+0,0) . " (address of LeftChild)"

MakeNode(Left) {
    ; Allocate 4 bytes of zero-initialized memory and store its address in temp2.
    temp2 := DllCall("GlobalAlloc", "uint", (GMEM_ZEROINIT:=0x40), "uint", 4)
    ; We must use +0 or similar, otherwise the address of the variable itself is used.
    NumPut(Left+0, temp2+0)
    msgbox % "infunction: " . NumGet(temp2+0,0) . " (address of LeftChild)"
    return temp2
}
Memory allocated by GlobalAlloc cannot be reclaimed until the application exits or you call GlobalFree:
Code:
DllCall("GlobalFree", "uint", Parent)
; Not really necessary, but can avoid some problems and help with debugging:
Parent := 0
There are alternatives to GlobalAlloc / GlobalFree:
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group