首先,我知道我的问题类似于以下发布的问题。 如何处理ruby Fiddle中的数组指针
但这个问题确实没有答案。我的问题是用 ruby 编写一个函数,它将接受一个浮点数的 ruby 数组作为输入并返回一个可以在 C DLL 中处理的 Fiddle::Pointer。就这个
# 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::Pointer。
谢谢!