On 16 May 2012, at 12:45, Mike Pall wrote: > William Adams wrote: >> I can do this: >> >> str = ffi.cast("char *", "Any old string") > > But you shouldn't. Strings are immutable, so that ought to be > "const char *". And in fact that cast is done implicitly, so you > don't need such a cast at all. > >> Does anyone know about the lifetime semantics of that string? >> Also, does this make a copy of the string, or is the 'str' >> variable now pointing at the in memory representation of the >> string. I'm assumig the former but I'm not sure. > > A cast NEVER makes a copy of anything. It converts one type of > pointer to another. And a Lua string is implicitly treated like a > "const char *" pointer to its first character. This is pretty much the same as the implicit operation that occurs when you pass a Lua string to an FFI function that requires a char *? Or does the string get copied to some intermediary buffer? > > >> Second, I can do this: >> >> buff = ffi.new("char[256]") >> >> buff[255]=0 >> >> luastring = ffi.string(buff) >> >> now, I have a lua string, which has been interned?, and the buff >> has been copied? > > Yes, ffi.string() interns a string. Yes, the part until the > terminator (or given length) of the string is copied to the > interned string. Is there any way to avoid interning the string, but still pass it as arguments to standard Lua functions (i'm guessing no)? For example using the build in io.write functions to write out the contents of a char array? -Arran