lua-users home
lua-l archive

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


>When I use:
>
>char *text;
>text = (char *)lua_tostring(L, 1);
>
>Does text now contain a pointer to the string or is it a pointer to a newly 
>allocated array of memory containing the sting.  I guess what I am asking is 
>does lua allocate some new space the I use lua_tostring.
>and does it ever de-allocate it or is it up to me?

The manual says:

 This function returns a pointer to a string inside the Lua environment.
 ...
 Because Lua has garbage collection, there is no guarantee that the
 pointer returned by lua_tostring will be valid after the respective
 value is removed from the stack.

which is not exactly what you want. The answer is that you do not need to free
anything (in fact, you must *not* free pointers returned by lua_tostring).
On the contrary, if you need the string later on, then you must duplicate it
yourself.
--lhf