recursive calls

I tried writing a floodFill program ( shown in another post ) using a recursive call, but I got a “stack overflow” error message and had to use a table instead. I was just wondering what the “stack overflow” limit was on other iPads. Run this code. The first time it will look like it’s not doing anything while doing the recursive calls. When you get the stack overflow message, exit back to the editor and run the program again. The first time the program is run, “recursive” in global data will be nil and it will do the recursive calls and save the count in global data. The second time, “recursive” will have a value that will print. On my 16gb iPad 1, it takes about 14 seconds before I get the “stack overflow” message. My recursive count is 16381. If you want to run this multiple times, uncomment the “saveGlobalData” line to set recursive to nil. Re-comment the line and run again.


function setup()
    --saveGlobalData("recursive",nil)    -- set recursive to nil
    a=readGlobalData("recursive")    -- read recurvive value
    print(a)    -- print nil or recursive value
    if a==nil then
        count=0    -- set count to 0
        recursive()    -- call recursive
    end
end

function recursive()
    count = count + 1    -- add 1 to count
    saveGlobalData("recursive",count)    -- save count in recursive key
    recursive()    -- call function recursive
end

The reason (at least, in this example) you are getting a stack overflow in the first place is because your recursive function has no “escape condition”. That is, once recursive() is called, it continues calling itself, forever, until you’ve blown the call stack.

EDIT: the maximum depth of the call stack usually shouldn’t matter (well, unless you are on a very constrained device)…the error here is the faulty recursive function.

I get 16381, too. I have a 32GB A1416.

@toadkick That’s the point of this program, to get the “stack overflow” error. I want to see if the memory size or iPad type has anything to do with the limit or is it a limit set in Codea. 2 so far with 16381. That’s just below hex 3FFF. So I think Codea has a stack limit of hex 3FFF or 16383.

@dave1707: Ha, oh :">

Then, success!

I also got 16381 on an iPad 4.

16 GB iPad 3 gets 16381, too.

I get 16381 on my Ipad 4, 32 GB

EDIT: Is it suppose to remove all your projects?..

EDIT2: It looks like it was just a Codea/iOS 7 glitch, no worries :slight_smile: