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

Shrinkage

Started by MrBarry, 21 September 2012 - 08:14 PM
MrBarry #1
Posted 21 September 2012 - 10:14 PM
So, I decided to see how many functions would fit on the stack, and I'm sort of perplexed by the result.


-- Blow your stack for educational purposes.
local _I = 1
local function stack_test()
  print("Your stack held " .. _I .. " functions.")
  _I = _I + 1
  stack_test()
end
The first time it is run, it prints up to 244 and then I get a vm error from bios line 40

java.lang.ArrayIndexOutOfBoundsException: 256.

So, maybe the stack size is 256?

But then, if I run it again without rebooting the computer, the vm error does not occur. It prints numbers up to 243 then writes "178", which cursory searches on this forum seem to suggest is an exit code.

Each subsequent run of this program prints one fewer lines.

My question is: Is this normal, or is this triggering some kind of memory leak? Am I not really overflowing the stack? Is the stack supposed to be cleared after the program terminates?
Noodle #2
Posted 22 September 2012 - 08:06 AM
You run the function inside the function.
Maybe try this
local _I = 1
while true do
  term.clear()
  term.setCursorPos(1,1)
  print("Your stack held ".. _I .." function.")
  _I = _I + 1
end
I believe it's normal.. It's just that you're running the function inside the function.
MrBarry #3
Posted 22 September 2012 - 10:29 AM
Yeah, I ran it that way on purpose to see how many open functions it would call before the stack overflowed. I just found it interesting that each time I ran it, it would loop one fewer times. Making the print statement smaller i.e. print(_I) or using write() didn't change the number of times it ran. It always goes 244 on a freshly booted computer, then 243 on the next run, and so on. Now that I think about it, I wonder what happens when I run it 244 times.
Zoinky #4
Posted 22 September 2012 - 12:05 PM
The stack is 256 large. Not sure why it stops at a lower number. Probably something to do with timing.
MrBarry #5
Posted 22 September 2012 - 07:22 PM
The os/shell would take up some space.