lua-users home
lua-l archive

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


Thanks a lot Pierre.

2012/2/21 Pierre Chapuis <catwell@archlinux.us>
On 2012-02-21 11:42, Olivier Goudron wrote:

I am testing LuaJIT FFI binding and i have a problem with this code :

****
local ffi = require("ffi")
ffi.cdef[[
struct slice{
 char *data;
 int len;
};
]]
local sk = ffi.new("struct slice")
sk.len = 3
sk.data = ""> print(sk.len)
print(ffi.string(sk.data))
****
The last line cause LuaJIT to crash and the "sk.data = "" assignement
complain with this message : cannot convert 'string' to 'char *'

LuaJIT is telling you what the problem is: you cannot just
assign a Lua string to a char* ctype. You need to allocate
the necessary memory and use ffi.copy.

tl;dr: replace the line:

   sk.data = "">
by something like:

   sk.data = "">    ffi.copy(sk.data,"key")

--
Pierre 'catwell' Chapuis




--
/*
Olivier Goudron, Formateur et Ingénieur système Linux.
Téléphone : 09 72 26 34 75
Email : olivier@goudron.fr
Site Web : http://goudron.fr
*/