AutoHotkey Community

It is currently May 26th, 2012, 6:32 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: June 10th, 2009, 8:18 pm 
Offline

Joined: December 18th, 2008, 9:03 pm
Posts: 54
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2009, 12:52 am 
Offline

Joined: December 18th, 2008, 9:03 pm
Posts: 54
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 :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2009, 3:23 am 
Offline

Joined: March 24th, 2004, 2:34 pm
Posts: 299
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 12th, 2009, 6:02 am 
Offline

Joined: December 18th, 2008, 9:03 pm
Posts: 54
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 12th, 2009, 9:50 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
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:


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: rbrtryn, Retro Gamer, SKAN, Yahoo [Bot] and 52 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