C++的代码:
int main()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L, "a.lua");
lua_getglobal(L, "test");
char* str = new char[3];
str[0] = 0;
str[1] = 65;
str[2] = 66;
lua_pushstring(L, str);
lua_pcall(L, 1, 1, 0);
const char* newP = lua_tostring(L, 1);
cout << newP[0] << newP[1] << newP[2];
lua_close(L);
return 0;
}
a.lua代码
function test(str)
return str
end
期待的值是\0AA
但是实际上 值已经乱了
是不是LUA 拷贝了一份char* 直到\0 字符的值
而不是传的 char*指针
谢谢大哥们了
str[0] = 0;
str[1] = 65;
str[2] = 66;
--->
str[0] = 65;
str[1] = 66;
str[2] = 0;