AutoIt will quit to prevent stack overflow

08/16/2017 00:59 .Hyleex.#1
Hello, I have the following problem with my bot:

AutoIt will quit to prevent stack overflow


My script:

I've already read a lot, and this problem is when calling functions from inside of functions and never allowing a function to fully complete and return, but in my case, i'm not calling functions.


I've read the article about Recursion in wiki


but i dont understand what i have to do to solve this problem...
08/16/2017 01:19 warfley#2
Quote:
but in my case, i'm not calling functions.
take a look at the end of the play function:
Code:
		Sleep(50)
		Send("f")
		Sleep(50)
	play()
	WEnd
EndFunc   ;==>Plcay
Before the WEnd you are calling play().

PS: normally AutoIt should give you this line in the error message
08/16/2017 01:46 .Hyleex.#3
Quote:
Originally Posted by warfley View Post
take a look at the end of the play function:
Code:
		Sleep(50)
		Send("f")
		Sleep(50)
	play()
	WEnd
EndFunc   ;==>Plcay
Before the WEnd you are calling play().

PS: normally AutoIt should give you this line in the error message
Hello, i've tried without the play() and i got the same error,

Is it possible to "erase" the stack? It seems that using the "return" I can "erase" this stack, how can I make it work? Thank you so much for helping
08/17/2017 00:41 elmarcia#4
Don't use while loop inside function if you want to pause your script later.
You should use boolean values to check when to work or not.

08/17/2017 04:16 .Hyleex.#5
Using Boolean I have to call the function, yes? Is there any way to I call functions inside a function and don't get stack overflow?
08/17/2017 11:42 florian0#6
Quote:
Originally Posted by .Hyleex. View Post
Is there any way to I call functions inside a function and don't get stack overflow?
Yes there is: Dont build infinite recursions! Or don't build recursions at all, because MOST of the time, they are completely unnecessary.
08/17/2017 13:35 .Hyleex.#7
Quote:
Originally Posted by florian0 View Post
Yes there is: Dont build infinite recursions! Or don't build recursions at all, because MOST of the time, they are completely unnecessary.

But if we consider it necessary to use recursion, how can I write the script so that I have no problems?
08/17/2017 13:59 florian0#8
Don't make them infinite. Make the recursion return before the stack overflows.
Also, most recursions can be replaced with a simple loop. To be more precise, all tail recursions can be resolved as loops (which is the case for your example).
08/17/2017 22:45 .Hyleex.#9
Quote:
Originally Posted by florian0 View Post
Don't make them infinite. Make the recursion return before the stack overflows.
Also, most recursions can be replaced with a simple loop. To be more precise, all tail recursions can be resolved as loops (which is the case for your example).
Hm... I can use return to prevent stack overflow, correct? I've read some texts about return but I haven't understand exactly how to use and how "return" works. Have a nice day!
08/17/2017 23:39 florian0#10
In general, "return" will leave a function; skipping what ever code is executing.

Lets say you have a simple pseudocode structure like this:
Code:
function DoMyStuff(coolnumber) {
    if (coolnumber < 5) {
        print "Cool Number is too small"
        return
    }

   print "Cool Number is :" + coolnumber
}
The return inside will directly leave the function. The second print is not executed.

For recursions, there is something to keep in mind:

Each function call will use some memory on the stack, for local variables, for parameters and the return. This is true for most (if not all) compiled languages, and also some script languages.

Code:
function MyStuff() {
    print "Hello World!"
    MyStuff() // <- recursive call
}
With each call to MyStuff, the stack ascends by at least the return address. If we run that program, it will run forever. But sadly, the size of the stack is limited. With each recursion, the stack gets fuller and fuller because the function never returns. And at some point, there is no memory left -> Stack overflow

Lets look at a different example: A formular for the greatest common divisor of two numbers based on Euklid.

Code:
function gcd(a, b) {
    if (b == 0) {
        return a
    }
    
    return gcd(b, a % b)
}
The recursion goes on and on, but at some point, b will become 0. If this occurs, the function will return without calling itself again. At this point, the recursion stops. The program descends through the stack.
There is still a chance that this function will overflow the stack. But unlike the first example, which had a 100% chance to overflow the stack, this one will only fail for really large numbers of recursions, aka. really large numbers as input.
08/18/2017 00:14 .Hyleex.#11
I think I understand...

in your first example, when you exit the function, it will return to the previous function who called DoMyStuff(), correct? so, if you create a "main" function who call every function, and every function have a break condition, (if $something = 1 then return) so, it will return to main function, and main function will decide what function to call,
will this solve the problem? i've read this here: [Only registered and activated users can see links. Click Here To Register...]

Can I solve the problem doing this?

Thank you so much for your help!
08/18/2017 23:58 florian0#12
Yes, this seems to be the same problem as yours.

I still think you don't need that recursion. Recursions are a special pattern that is only useful for solving certain problems (like calculating the fibonacci number). Your program does not make use of the recursion (does not pass data, recursion has no real impact on the codeflow. The recursion is, in your case, only a simple loop.

Like my example:
Code:
function MyStuff() {
    print "Hello World!"
    MyStuff() // <- recursive call
}
is just another form of
Code:
function MyStuff() {
    while (true) {
        print "Hello World!"
    }
}
08/21/2017 02:56 .Hyleex.#13
Quote:
Originally Posted by florian0 View Post
Yes, this seems to be the same problem as yours.

I still think you don't need that recursion. Recursions are a special pattern that is only useful for solving certain problems (like calculating the fibonacci number). Your program does not make use of the recursion (does not pass data, recursion has no real impact on the codeflow. The recursion is, in your case, only a simple loop.

Like my example:
Code:
function MyStuff() {
    print "Hello World!"
    MyStuff() // <- recursive call
}
is just another form of
Code:
function MyStuff() {
    while (true) {
        print "Hello World!"
    }
}
i wanna understand how recursion works, and how i can avoid this problem for future projects, thank you!