lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Sat, May 7, 2011 at 4:58 PM, Emmanuel Oga <> wrote:
> This version though, like yours, just keeps making the stack grow with
> each recursion.

Your question was confusing, it sounded like you were concerned about
the lua stack, but its the C stack you are concerned with.

> About the stack, is there a hard limit on its size or can I grow it
> until there are no longer any available system memory?

C stack size is large, but limited, and hard to test how close to the
end of it you are, at least portably.

You can trivially (though tediously) turn your recursive code into
iterative code. Find all the local variables in fillTree(), and move
them into a struct. For every current call to fillTree(),instead
malloc() a new struct, push it onto a stack of the structs, and store
your local variables into it so you can pop them later, and continue
looping. You are essentially building your own stack in the heap.
Malloc failure is detectable by your code, so you can recover in
whatever sense you think is graceful.

If you system allows, you'd be better off binding opendir()/readdir()
into lua, and writing the whole thing in lua (or using luaposix of