lua-users home
lua-l archive

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


Denis Lamarche wrote:
> 
> 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?

You get a pointer to the string.  Lua will not create a copy for you.
And if Lua does not use the string any more it will free it.  So you
may use 'text' only when you are sure that Lua still has a reference
to the string (i.e. it's still on the stack or table or ...).

A note: lua_tostring returns a 'const char *' because it is a const
string.  You are not allowed to modify the it.

Ciao, ET.