Fiddle - Pass pointer to function

Hi,

In FFI to pass a pointer to a C function I would do something like:

arr = [1.0, 2.0, 3.0]
vec_pointer = FFI::MemoryPointer.new(:double, arr.size)
vec_pointer.put_array_of_double(0, arr)

How do I do the same using Fiddle?

I have tried the following with no success

s = Fiddle::Function.new(slib[‘add_vector’], [Fiddle::TYPE_VOIDP,
Fiddle::TYPE_INT], Fiddle::TYPE_DOUBLE)
a = [1.0, 2.0, 3.0]
arr_ptr_int = a.object_id << 1
arr_ptr = Fiddle::Pointer.new arr_ptr_int, 3 * Fiddle::SIZEOF_FLOAT

The C function is called, but the content of the array(pointer) is just
random number and I guess this means that I am passing the wrong
address.

Thanks!

Just in case this could help someone, the way to pass an array pointer
is

arr = [1.0, 2.0, 3.0]
ptr = Fiddle::Pointer[arr.pack(‘E*’)]

The E3 is for little endian byte order

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs