Re: luajit string to char * conversion

  • From: marcelo nicolet <marcelo.nicolet@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 19 Nov 2014 19:20:08 -0300

I catched the trick: when the argument will not be modified, declaring it const does the job. But when it will be modified (as in my example for recv() ) to have in the middle of lua code a call to malloc() and then to free() is not a "good thing" to my taste.

What about
local v = ffi.new( "char[?]", bufsize )
Is it ok?
TIA

PS: what if malloc() cannot allocate any memory? NULL from malloc comes as nil to lua code?


On 11/19/2014 04:49 PM, Florian Weimer wrote:
* marcelo nicolet:

This answer was right: -Wl, -E did the trick.

Now, being the function create_thread( char * ), and calling it as
C.create_thread( lua_string ) it gives

./mainthread: second.lua:33: bad argument #1 to 'create_thread' (cannot convert
'string' to 'char *')

What can I do? How can I convert the lua string to char*
You need to call strdup (and free the pointer somewhere) if the
create_thread function indeed modifies the argument string.
Otherwise, it is sufficient to change the type of the argument to
“const char *” (both in the Lua declaration and the C implementation).
Lua strings must not be modified, and the C type system enforces this
for once.

(ffi.cast is the wrong way to tackle this.)





Other related posts: