Ruby Fiddle - 将数组传递给C函数

时间:2016-04-26 06:58:06

标签: c ruby fiddle

我正在尝试使用Fiddle(2.0.0-p247)将数组传递给C函数。这是我的ruby脚本

require 'fiddle'
clib = Fiddle.dlopen('sum_array')
f = Fiddle::Function.new(clib['sum_array'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT],  Fiddle::TYPE_INT)
a = [1,2,3]
ptr = Fiddle::Pointer.new(a.object_id)
puts "Sum is #{f.call(ptr,3)}"

我还尝试用

替换Pointer定义
ptr = Fiddle::Pointer.new(a.object_id, 3 * Fiddle::SIZEOF_INT)

C函数如下:

int sum_array(int a[], int num_elements)
{
   int i, sum=0;
   for (i=0; i<num_elements; i++)
   {
     sum = sum + a[i];
   }
   return(sum);
}

当我运行脚本时,它会崩溃。 This是崩溃报告。

0 个答案:

没有答案