Re: newbie: local buf = ffi.new("char[?]", "HELLO WORLD") fails PROBABLY SOLVED

  • From: "Robert G. Jakabosky" <bobby@xxxxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 21 Jun 2012 07:28:48 -0700

On Thursday 21, Patrick Masotta wrote:
> good question...
> so far I run into trouble sending ints to the C side; I have done so
> on a C log function (like printf but for GUI) that receives variable # of
> parameters. The string pointers where passed ok but the integers weren't.
> So far I've solved the thing with a trick
> ffi.C.LOG(l, string.format(...))
> buy why your question? why I couldn't pass the integers??

The reason for my question about passing the limit to the C side is that 
seeing code that uses strcpy/strcat makes me nervous, since it is very easy to 
overflow the buffer.

Ah, I wasn't thinking about the issue of passing Lua numbers to vararg 
functions.  I had just thrown that example together.  The FFI docs say you 
have to cast the Lua number to an int, since LuaJIT doesn't know that the 
function wants an int instead of a double.

-- Example from the FFI docs:
ffi.C.printf("integer value: %d\n", ffi.new("int", x))


-- 
Robert G. Jakabosky

Other related posts: