I'm embedding Lua in a C++ application using Lua5.1 and I'm having an odd issue with luaL_newstate().
This works:
lua_State *L = NULL;
int main()
{
L = luaL_newstate();
return 0;
}
I recently restructured my code and chose to create an init function like this:
lua_State *L = NULL;
void init_lua(lua_State *L)
{
L = luaL_newstate();
}
int main()
{
init_lua(L);
return 0;
}
That doesn't work. For some reason, luaL_newstate() always returns NULL in that situation. But, to add to the confusion, this does work:
lua_State *L = NULL;
void init_lua(lua_State **L)
{
*L = luaL_newstate();
}
int main()
{
init_lua(&L);
return 0;
}
Functionally, I don't see a difference between the second and third examples and yet the second segfaults as soon as I attempt a lua call using L and the third works just fine. What is happening here?