1
votes
  1. a lua function return two number
  2. c++ calls the lua function: foo
  3. I don't know whether it's needful to pop the return value of function foo(lua_pop(L,2);).
  4. please tell me how to do it and why. thanks very much.

part code like this:

// lua function
function foo(a, b)
    return a+b, a-b;
end

// c++

lua_getglobal(L,"foo"); // push function
lua_pushnumber(L,1);    // push argument 1
lua_pushnumber(L,2);    // push argument 2

error=lua_pcall(L, 2, 2, 0);

if (!error) {
    printf("return:%s\n",lua_tostring(L,-1));
    printf("return:%s\n",lua_tostring(L,-2));
    // is this needful
    lua_pop(L,2);
}
1

1 Answers

1
votes

You should always try and keep the stack in a known state, in case you call more functions using the same lua_State. If you leave results sitting on the stack and make more calls, you will eventually fill the available stack space up.

So yes, you should pop the 2 results off the stack after using their values.