This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ChaddJackson12's profile picture

Stack Overflow Error Check (Make sure it doesn't happen)[help]

Started by ChaddJackson12, 18 October 2012 - 05:27 PM
ChaddJackson12 #1
Posted 18 October 2012 - 07:27 PM
Ok, so I am making this program. I am trying NOT to have the stack overflow error, and so this is what I am doing:


local UseProgram = true

function Exit()
   UseProgram = false
   term.clear()
   term.setCursorPos(1,1)
   shell.run("AnotherProgram")
end

function Program2()
   input = read()
   if input == "yay" then
	  Exit()
   else
	  print("Yay!")
	  Exit()
   end
end

end

function Program()
   input = read()
   if input == "yay" then
	  Program2()
   else
	  Exit()
   end
end

while UseProgram do
   Program()
end

Basically it does that, but, the other functions that it runs only are used once when the program it running, then it goes into another program. Will this cause a stack overflow? Or avoid one? Please help, thanks in advance.
remiX #2
Posted 18 October 2012 - 08:40 PM
Yeah that should prevent stack overflows if I'm correct.

Why not just use while true do and then when you call exit make it return false?
ChaddJackson12 #3
Posted 18 October 2012 - 08:49 PM
Yeah that should prevent stack overflows if I'm correct.

Why not just use while true do and then when you call exit make it return false?

How would I go about doing this? :3 I am not good with while loops :P/>/>
remiX #4
Posted 18 October 2012 - 08:51 PM
This should work


function Exit()
   UseProgram = false
   term.clear()
   term.setCursorPos(1,1)
   shell.run("AnotherProgram")
   return false
end

function Program2()
   input = read()
   if input == "yay" then
	 Exit()
   else
	 print("Yay!")
	 Exit()
   end
end

end

while true do
   input = read()
   if input == "yay" then
	 Program2()
   else
     Exit()
   end
end
ChaddJackson12 #5
Posted 18 October 2012 - 09:00 PM
This should work


function Exit()
   UseProgram = false
   term.clear()
   term.setCursorPos(1,1)
   shell.run("AnotherProgram")
   return false
end

function Program2()
   input = read()
   if input == "yay" then
	 Exit()
   else
	 print("Yay!")
	 Exit()
   end
end

end

while true do
   input = read()
   if input == "yay" then
	 Program2()
   else
	 Exit()
   end
end
So I could take out the "DoProgram" and the "DoProgram = false" then just do "return false"?
remiX #6
Posted 18 October 2012 - 09:39 PM
So I could take out the "DoProgram" and the "DoProgram = false" then just do "return false"?

Yep, just removes some unnecessary lines.
Cloudy #7
Posted 18 October 2012 - 09:44 PM
If another program is ran in another program which is ran in another program you will eventually get a stack overflow - it would just take a while.
remiX #8
Posted 18 October 2012 - 09:45 PM
If another program is ran in another program which is ran in another program you will eventually get a stack overflow - it would just take a while.

Programception.

But true true - how long exactly would it take?
ChaddJackson12 #9
Posted 18 October 2012 - 09:45 PM
If another program is ran in another program which is ran in another program you will eventually get a stack overflow - it would just take a while.
What I am doing is having the other programs it runs have the same thing this does. With the while loop. Will this still cause a stack overflow?
Cloudy #10
Posted 19 October 2012 - 01:29 AM
If another program is ran in another program which is ran in another program you will eventually get a stack overflow - it would just take a while.
What I am doing is having the other programs it runs have the same thing this does. With the while loop. Will this still cause a stack overflow?

Yes. Eventually, since you'll fill up the stack with what was running before. Are you just wanting to exit the program entirely or start again?
ChaddJackson12 #11
Posted 19 October 2012 - 01:51 AM
Well I am planning on having to exit back to a menu to execute other programs, so I guess exit it entirely until I want to reurn to it from the list.
Orwell #12
Posted 19 October 2012 - 01:55 AM
Well I am planning on having to exit back to a menu to execute other programs, so I guess exit it entirely until I want to reurn to it from the list.

You can just use the 'return' statement from the other program (outside any function). It will exit and your code will resume from the line that started this other program.