gpt4 book ai didi

c - 使用 Fiddle 将 RUBY 数组传递给 C DLL

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:59 25 4
gpt4 key购买 nike

首先,我知道我的问题与下面发布的问题类似。 How to deal with array pointers in ruby Fiddle

但这个问题确实没有答案。我的问题是用 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);

该函数已经用Fiddle::Importer 包装了,这与上一个问题相同。所以问题是如何在 ruby​​ 代码中初始化 Fiddle::Pointer。

谢谢!

最佳答案

arr = [1.0, 2.0, 3.0]
ptr = Fiddle::Pointer[arr.pack('E*')]

E* 是小端字节序,

从这个 Ruby forum link 得到以上内容

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

关于c - 使用 Fiddle 将 RUBY 数组传递给 C DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35192214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com