lua-users home
lua-l archive

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

On Tue, May 15, 2012 at 6:53 PM, William Riancho <> wrote:
> I have a problem using the Lua C API. When lua_pcall fails, the error
> is pushed on the stack. So when you pop the stack you get the error
> but when you count elements in stack you get 0.
> #include <lua5.2/lauxlib.h>
> #include <lua5.2/lua.h>
> #include <lua5.2/lualib.h>
> int main()
> {
>     lua_State *L = luaL_newstate();
>     lua_pcall(L, 0, 0, 0);
>     printf("%d %s\n", lua_gettop(L), lua_tostring(L, -1));
> }

Before lua_pcall, the stack is empty. lua_pcall will pop the function
to be called, and then (in this case) push an error message. The net
effect of one pop and one push is that the stack is still empty. That
said, you're inducing a pop on an empty stack, and all subsequent
behaviour is undefined