p4010
p4010

Reputation: 943

How to deal with array pointers in ruby Fiddle

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!

Upvotes: 2

Views: 1163

Answers (1)

JoeWoo
JoeWoo

Reputation: 66

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.

Upvotes: 2

Related Questions