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

  • From: Michal Kottman <k0mpjut0r@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 21 Jun 2012 14:50:51 +0200

On 21 June 2012 14:41, Patrick Masotta <masottaus@xxxxxxxxx> wrote:
> (Sorry about the HTM and top-posting...)
>
> I just wanted to avoid the LUA string creation.
> To me it does not make much sense creating a LUA string to define a C 
> string...

It's Lua, not LUA. Regarding the "Lua string creation" - what you have
to understand is that the Lua string is already created - all string
literals are created as constants when the code is compiled. This
means that when you call this:

local buf = ffi.new("char[?]",0, "HELLO WORLD")

The string "HELLO WORLD" is already somewhere in memory. What FFI does
is that it copies this string over a 0-byte char array (overwriting
memory you should not be writing to).

Other related posts: