lua-users home
lua-l archive

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

On Thu, Dec 15, 2011 at 07:22:56PM +0530, rahul sharma wrote:
> Hi All,
> Thanks for your kind reply. Why I want the address in Lua is because I want
> to know the address in Lua, then I will pass that string to C, and in C, I
> will get the address of that string using lua_tostring(). So if these two
> match, then I can be sure that no copying is done in lua on passing string
> from Lua to C.

You can't do this.  You can write a function in C that is callable from
Lua, to which you pass a string.  This function can then obtain a
pointer to the string using lua_tostring().  Obtaining the "pointer" to
the string data and then passing that pointer to a function would mean
that the string may get garbage collected inbetween your calls, meaning
the pointer would point to old, invalid data.

The only time Lua copies strings is when they are created.  After that,
as they are immutable, they are always passed by reference.  If you pass
a Lua string to a C function, it will be such a reference, and not a