1
首先我知道我的問題類似於以下發布的問題。 How to deal with array pointers in ruby Fiddle使用小提琴將RUBY數組傳遞給C DLL
但這個問題確實沒有回答。我的問題是在ruby中編寫一個函數,它將接受一個ruby的float數組作爲輸入並返回一個可以在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);
的功能已被包裹着小提琴::進口商,這是一樣的前一個問題。所以問題是我如何初始化ruby代碼中的Fiddle :: Pointer。
謝謝!
能否請您解釋拆包爲什麼它被投下來?我最近將我的一個庫從FFI移到了小提琴上,並使用上面的代碼創建了一個數組指針,它工作得很好。 – Jara