lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I don't know the answer to this, but i would always make the assumption that it's not safe. The Lua guys have always said that the implementation of the VM is not something static, and might change in the future, so, even if it may be safe to do so right now, i wouldn't bet on it working in the future.

But, to my knowledge you can do this:

int SomeFunction( lua_State * L )
{
   lua_getfield( L, 1, "name" );
   const char * name = lua_tostring( L, -1 );

   UseItInAFunction( name );

   lua_pop( L, 1 );
   return 0;
}

// David Morris-Oliveros
// Camera & Lua Coder
// david@teambondi.com



Chris wrote:
Is it safe to use string values retrieved from a table even though the value is popped off the stack? For example:

Lua side:

table = { name="test1234" }

C side:

/* assume "table" is passed as argument to this function */
int SomeFunction(lua_State* L)
{
   lua_getfield(L, 1, "name");
   const char* name = lua_tostring(L, -1);
   lua_pop(L, 1);

   ... use "name" char* variable...
}

I know this is not always safe because the garbage collector might take the string after it is popped off this stack but in this case the string still exists in the table and will not be collected until "table" is collected (table is still on the stack). So is this example safe or is the string from lua_getfield actually a different string than the one in the table?

--
// Chris