1

I am making a simple number cracker inside of Lua and i encountered a "Stack Overflow" error(Oh the irony). I am not 100% what this error actually is as when i google it all i get is this site which iv been using for a while. I want to have a way to clear the memory in the lua file. This program runs through numbers very fast and it can run these numbers up to 2147483647 times so im assuming this is a memory issue. So is there a way to clear the memory in a lua script whilst still running the script? here is my code:

num = 0
rand = math.random(2147483647)
function Main()
    print ("Please enter your number(0 - 2147483647)")
    ui = io.read("*number")
    loop()
end
function loop()
    if rand > ui and rand ~= ui then
        rand = math.random(0, ui)
        num = num + 1
        print(rand)
    end
    if rand < ui and rand ~= ui then
        rand = math.random(ui, 2147483647)
        num = num + 1
        print(rand)
    end
    if ui ~= rand then
        loop()
    end
    if ui == rand then
        print("Number Cracked - " ..ui)
        print("It Took " ..num .." Trys To Crack Your Number")
        done = io.read()
    end
end
Main()

EDIT

Thanks to @Marc B and @Blaatz0r for commenting the answers, i was calling loop() to many times and it was causing, well a "Stack Overflow", i replace it with a while loop, thanks to @Marc B for that, here is my new code:

num = 0
rand = math.random(2147483647)
function Main()
print ("Please enter your number(0 - 2147483647)")
ui = io.read("*number")
while( ui ~= rand) do
if rand > ui and rand ~= ui then
rand = math.random(0, ui)
num = num + 1
print(rand)
end
if rand < ui and rand ~= ui then
rand = math.random(ui, 2147483647)
num = num + 1
print(rand)
end
if ui == rand then
print("Number Cracked - " ..ui)
print("It Took " ..num .." Trys To Crack Your Number")
done = io.read()
end
end
end
Main()
Cubit-Games
  • 75
  • 2
  • 9
  • you just keep calling loop() over and over... eventually you will overflow your stack. why does have to be recursive in the first place? wouldn't a `while` loop be more efficient? – Marc B Jan 14 '16 at 22:01
  • It keeps calling loop(). Therefor all these calls are set to the stack. In time it overflows. Hence stack overflow. You should try to find some info on stack vs heap. http://stackoverflow.com/q/79923/3966890 . – Blaatz0r Jan 14 '16 at 22:02
  • Thank you both :D i got my code working, now i can stare at numbers scrolling by all day, thanks :D – Cubit-Games Jan 14 '16 at 22:14
  • 1
    Note that you did not have to rewrite the whole code as a while loop to solve the issue. Lua has proper tail calls (http://www.lua.org/manual/5.3/manual.html#3.4.10), so you could just replace the recursive call to `loop()` by `return loop()`. – catwell Jan 15 '16 at 01:47
  • I think the title here is a little misleading – warspyking Jan 15 '16 at 01:50
  • @Cubit-Games: Don't put your answer in your question. Questions are for *questions*; if you have an answer you want to contribute, write an answer. – Nicol Bolas Jan 15 '16 at 02:50
  • iv actually been waiting so it will let me answer my own question, it makes you wait like 12 hours or so, also yall could have answered it in an answer instead of a comment :/ – Cubit-Games Jan 15 '16 at 20:00

1 Answers1

0

Thanks to @Marc B and @Blaatz0r for commenting the answers, i was calling loop() to many times and it was causing, well a "Stack Overflow", i replace it with a while loop, thanks to @Marc B for that, here is my new code:

num = 0
rand = math.random(2147483647)
function Main()
    print ("Please enter your number(0 - 2147483647)")
    ui = io.read("*number")
    while( ui ~= rand) do
        if rand > ui and rand ~= ui then
            rand = math.random(0, ui)
            num = num + 1
            print(rand)
        end
        if rand < ui and rand ~= ui then
            rand = math.random(ui, 2147483647)
            num = num + 1
            print(rand)
        end
        if ui == rand then
            print("Number Cracked - " ..ui)
            print("It Took " ..num .." Trys To Crack Your Number")
            done = io.read()
        end
    end
end
Main()
Cubit-Games
  • 75
  • 2
  • 9