Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用FIDLE将RUBY数组传递给C DLL_C_Ruby_Dll_Fiddle - Fatal编程技术网

使用FIDLE将RUBY数组传递给C DLL

使用FIDLE将RUBY数组传递给C DLL,c,ruby,dll,fiddle,C,Ruby,Dll,Fiddle,首先,我知道我的问题与下面的问题相似。 但这个问题确实没有得到回答。我的问题是用ruby编写一个函数,它将接受一个ruby浮点数组作为输入,并返回一个可以在C DLL中处理的Fiddle::指针。给你 # transform an ruby array of double to Fiddle::Pointer and processed as double* in C DLL # @param [Array] array the ruby array, e.g. [1.0, 2.0, 3.0]

首先,我知道我的问题与下面的问题相似。

但这个问题确实没有得到回答。我的问题是用ruby编写一个函数,它将接受一个ruby浮点数组作为输入,并返回一个可以在C DLL中处理的Fiddle::指针。给你

# transform an ruby array of double to Fiddle::Pointer and processed as double* in C DLL
# @param [Array] array the ruby array, e.g. [1.0, 2.0, 3.0]
# @return [Fiddle::Pointer] fiddle pointer that could be processed as double * in C
def double_array_to_ptr(array)
  length = array.length
  ptr = Fiddle::Pointer.malloc(length * Fiddle::SIZEOF_DOUBLE)

  # initialize the ptr with numbers in array
  for i in 0..length-1
    # how to initialize the ptr
    # maybe ptr[start, length] would help, I have tried but no success

  end
  return ptr
end
C函数可能是这样的

/**
 * \brief       H2OSetGlobalOffset set the global offset of data
 * \param[in]   H2OProjectH project project manager, this one has been initialized
 * \param[in]   double * offset offset data, the buffer is initilized in ruby 
*/
H2O_EXPORT void H2OSetGlobalOffset(H2OProjectH project, double* offset);
该函数已用Fiddle::Importer包装,这与前面的问题相同。所以问题是如何在ruby代码中初始化Fiddle::指针

谢谢

arr = [1.0, 2.0, 3.0]
ptr = Fiddle::Pointer[arr.pack('E*')]
E*
用于小尾端字节顺序

从这上面得到的


有关不同数据类型的打包和解包的更多信息

请您解释一下为什么被否决?我最近将我的一个库从FFI迁移到了fiddle,并使用上面的代码创建了一个数组指针,它工作得很好。