c调用lua函数,返回值在堆栈上。我需要弹出它吗? 一个lua函数返回两个数字 C++调用LUA函数:FO 我不知道是否需要弹出函数duffua_popL,2;的返回值;。 请告诉我怎么做,为什么。非常感谢。

c调用lua函数,返回值在堆栈上。我需要弹出它吗? 一个lua函数返回两个数字 C++调用LUA函数:FO 我不知道是否需要弹出函数duffua_popL,2;的返回值;。 请告诉我怎么做,为什么。非常感谢。,lua,Lua,部件代码如下: // 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) {

部件代码如下:

// 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);
}

您应该始终尝试将堆栈保持在已知状态,以防使用相同的lua_状态调用更多函数。如果将结果留在堆栈上并进行更多调用,则最终将填满可用的堆栈空间


因此,是的,您应该在使用两个结果的值后将它们从堆栈中弹出。

确实如此。如果您懒惰,lua_pop l,lua_gettopl将从堆栈中弹出所有剩余的条目。lua_settopL,0似乎更习惯于清除堆栈