[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: stack overflow?
- From: Rici Lake <lua@...>
- Date: Tue, 30 Aug 2005 22:14:30 -0500
On 30-Aug-05, at 9:24 PM, Mike Pall wrote:
Hi,
Rici Lake wrote:
By the way, the largest possible Lua stack frame is 255 slots.
(LUA_MAXSTACK + LUA_EXTRA_STACK, but don't change these!)
255 is just the limit for the directly addressable slots
(aka local variables). A Lua function will happily pass
on any number for open operations (return f(), g(f()),
{f()}, return ..., f(...), {...}).
Examples (Lua 5.1 required):
$ lua -e 'print(string.byte(string.rep("\0", 1000), 1, -1))'
$ lua -e 'print(#{string.byte(string.rep("\0", 1000), 1, -1)})'
[The limit here is LUAI_MAXCSTACK (2048).]
That's correct, but I think it is a shade of meaning. The extra stack
slots here come from the following stack frame, in effect; they could
not be there had they not been created in the following stack. So one
reading of what is happening here is that the downstream stack frame is
being merged with the current stack frame and the merger is being
returned (or used as a table constructor). In any event, the size of
the total stack cannot exceed the maximum recursion depth * the maximum
stack size for each frame (calculated independently for Lua frames and
C frames.)
I don't know if that was clear, but it made sense to me :)
But we can do better:
local function foo(n, ...)
if n == 0 then return ... else return foo(n-1, 0, ...) end
end
print(#{foo(30000)})
[This will take a long time.]
That is an exception I had not contemplated, indeed. I don't know that
there is a limit to the number of varargs, but it really ought to be
LUAI_MAXCSTACK.
And unless I'm mistaken there is no relationship between
MAXSTACK and EXTRA_STACK.
I never said there was. I just said that the sum of those two values in
the default Lua happens to be 255. Or, rather, I meant to say that. :)
Only MAXSTACK has to be <= MAXARG_A
(255). EXTRA_STACK is used for easier calling of metamethods.
There are no arithmetics involved that would truncate the
sum of those two to 8 bits.
Please correct me if I'm wrong, because I increased EXTRA_STACK
(for a good, but complicated to explain reason).
Bye,
Mike