我正在尝试使用 Fiddle (2.0.0-p247) 将数组传递给 C 函数。这是我的红宝石脚本
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);
}
当我运行脚本时它崩溃了。这是崩溃报告。