Re: what exactly happens with string casts?

  • From: Dimiter 'malkia' Stanev <malkia@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 16 May 2012 00:18:09 -0700

My understanding is this:

On 5/15/2012 10:19 PM, William Adams wrote:
I can do this:



str = ffi.cast("char *", "Any old string")
This ffi string would live as long as "str" itself lives.
For example
do
   local str = ffi.cast( "char *", "Any old string" )
end
-- here it might be gone




And that's a quick and dirty way to get a pointer to a string.  Or to be more 
precise, a pointer on a bit of memory that has some characters in it.



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.



Second, I can do this:



buff = ffi.new("char[256]")
This would create an ffi object that is array of 256 characters, all zero

buff[255]=0
I guess then this would not have an effect.


luastring = ffi.string(buff)
Most likely just an empty string, returned in luastring


now, I have a lua string, which has been interned?, and the buff has been 
copied?
It's interned, as much as luastring = "" is interned. The buff was "copied" but actually zero bytes were copied from there. But most likely the buff[0] was accessed.




These are just some assumptions I need some clarification on.



-- William

===============================

- Shaping clay is easier than digging it out of the ground.


http://williamaadams.wordpress.com
http://www.thingiverse.com/WilliamAAdams
https://github.com/Wiladams                                     


Other related posts: