[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How is string passed from Lua to C
- From: rahul sharma <rahulatgslab@...>
- Date: Wed, 7 Dec 2011 17:57:44 +0530
> 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.
I couldn't get the fact of using it in C after we return from C. From Lua function, I am calling the C function by passing values to it. So when my program enters C, we get a C stack created, so that C stack is used by C program to access values of Lua. So in case of string, is the C stack able to access the Lua address space where I have stored the string? You can see my question with code here on this link as well...
Thanks and Regards