2

I am trying to switch from Ruby FFI to Fiddle, which is now part of Ruby std lib.

It is pretty undocumented though, and I am having a hard time in figuring out how to deal with arrays and pointers. In particular, how can I write a Fiddle interface to a C function like this:

void my_func(double *, size_t len)

Mapping it into Ruby is pretty easy:

module Test
  extend Fiddle::Importer
  dlload './lib/libTest.dylib'
  extern 'void my_func(double *, size_t)'
end

But then how can I build a pointer to an array, to be passed as a first argument? Thanks!

1 Answer 1

2

you mean build an c array and this array`s pointer in ruby?

you can try like this:

free = Fiddle::Function.new(Fiddle::RUBY_FREE, [TYPE_VOIDP], TYPE_VOID)
p = Pointer.malloc(SIZEOF_DOUBLE*len, free)

and call like this:

my_func(p,len)

ruby GC will call the free function when this memory block will not be used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.