Lua C API 不适用于指针
Lua C API doesn't work with pointer
这就是我所做的。我在C++代码中创建了一个结构和一个静态函数
struct ItemDefinition {
int64_t price;
};
int static getPrice(lua_State* L) {
ItemDefinition* def = (ItemDefinition*)lua_touserdata(L, 1);
printf("%pn", def);
printf("%dn", def->price);
return 0;
}
我还在lua中注册了该函数并初始化了一个ItemDefination对象。
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的地址,如果工作正常。所以我确定该函数被成功调用。但是,如果我试图获得def->price
,我会从地址清理器收到错误"==283789==ERROR stack use after return"
我不熟悉 c++ 和 lua。你能在问题所在提供帮助吗?
解决此问题,NVM。这实际上是因为指针问题。
相关文章:
- OpenGL - 在 NDC 中计算位置适用于着色器,但不适用于'regular'程序
- 使用模板参数重载C++方法:如何使其适用于模板的子类?
- 如何修复我的最大公约数代码?它适用于除零和零以外的所有数字
- 选择排序C++(已修改)并非适用于所有情况
- 无法让"std::enable_if"适用于无作用域枚举
- 请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例
- 确定夏令时是否适用于特定日期
- 是否有一种 STL 算法可以最后找到,但它也适用于指针?
- QT 样式表主题,适用于使用属性选择器的整个应用程序
- C++带有适用于左值和右值的引用参数的函数
- 代码适用于调试,但不适用于发布
- C++17 和更新的 std::分配器是否适用于动态数量的自定义堆?
- 指针算法是否适用于迭代器?
- 动态强制转换适用于模板成员函数内的共享指针和弱指针,无需代码重复
- 为什么"return 0"适用于在函数中返回指针,但不适用于参考?
- STL 算法 + c'tor 不再适用于指向 C 数组的原始指针,因为指向一个过去的末端
- 指向成员的指针的语法糖适用于数组,但不适用于 std::vector
- 模板仅适用于智能指针
- delete[]操作符是否适用于通过指针返回的动态分配的内存?
- 为什么函数指针定义适用于任意数量的'&'号或星号"*"?