[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How is string passed from Lua to C
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Wed, 7 Dec 2011 08:44:19 -0200
> Can anyone explain me how the string fetching is done by Lua. I want to
> pass string from Lua to C. I am able to access it using gettable and then
> converting it to string using lua_tostring(). But I don't know whether a
> new copy of that string is made on stack or its a reference to the string's
> location. If anyone knows about it, do let me know. My basic motto is to
> avoid the copy of data.
lua_tostring returns a reference to a string inside Lua. There is no copy.
However, strings are subject to garbage collection and so, unless you're
positive that the strings is and will remain referenced in Lua, you should
make your own copy if you plan to use it in C after you return from C. In
a C function there is no need to duplicate Lua strings, because they are
anchored in the stack.