2016-02-04 27 views
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。

谢谢!

回答

-1

ARR = [1.0,2.0,3.0]

PTR =小提琴::指针[arr.pack( 'E *')]

的E *为小端字节顺序,

得到了这个Ruby forum link

上述了解更多关于包装和不同的数据类型check this post

+0

能否请您解释拆包为什么它被投下来?我最近将我的一个库从FFI移到了小提琴上,并使用上面的代码创建了一个数组指针,它工作得很好。 – Jara