C++ luacapi不';不能使用指针
这就是我所做的。 我在C++代码中创建了一个结构和一个静态函数C++ luacapi不';不能使用指针,c++,lua,c,C++,Lua,C,这就是我所做的。 我在C++代码中创建了一个结构和一个静态函数 struct ItemDefinition { int64_t price; }; int static getPrice(lua_State* L) { ItemDefinition* def = (ItemDefinition*)lua_touserdata(L, 1); printf("%p\n", def); printf("%d\n", def->price); return 0; } 我还在
struct ItemDefinition {
int64_t price;
};
int static getPrice(lua_State* L) {
ItemDefinition* def = (ItemDefinition*)lua_touserdata(L, 1);
printf("%p\n", def);
printf("%d\n", def->price);
return 0;
}
我还在lua中注册了这个函数,并初始化了一个ItemDefinition对象
ItemDefinition def;
def.price = 120;
lua_pushlightuserdata(lua_state_, (ItemDefinition*)&def);
lua_setglobal(lua_state_, "def");
luaL_Reg module[] = {{"getPrice", &getPrice}, {NULL, NULL}};
从Lua脚本中,我只需调用函数
getPrice(def)
如果我只是打印出def的地址,if工作正常。因此,我确信函数调用成功。但是,如果我试图获取def->price
,我将从地址消毒剂“==283789==error stack use after return”
我对和都不熟悉。您能帮助解决问题吗?解决问题,NVM。这实际上是因为指针问题。格式说明符
%d
对于类型int64\t
不正确。顺便说一句,我正在尝试创建一个全局对象,在我的示例中是“def”,它将被多次调用。。因此,测试实际上多次调用getPrice方法。我不确定这种行为是否会导致问题?请。