如何应对红宝石小提琴指针数组

编程入门 行业动态 更新时间:2024-10-25 17:25:10
本文介绍了如何应对红宝石小提琴指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从Ruby的FFI乱动,也就是现在的Ruby标准库的一部分进行切换。

I am trying to switch from Ruby FFI to Fiddle, which is now part of Ruby std lib.

有虽然无证pretty,和我有搞清楚如何处理数组和指针之苦。特别是,我怎么能写一个小提琴接口,这样的C函数:

It is pretty undocumented though, and I am having a hard time in figuring out how to deal with arrays and pointers. In particular, how can I write a Fiddle interface to a C function like this:

void my_func(double *, size_t len)

映射入Ruby是pretty容易:

Mapping it into Ruby is pretty easy:

module Test extend Fiddle::Importer dlload './lib/libTest.dylib' extern 'void my_func(double *, size_t)' end

不过,我又如何能建立一个指向数组的指针,将作为第一个参数传递?谢谢!

But then how can I build a pointer to an array, to be passed as a first argument? Thanks!

推荐答案

你的意思是建立一个数组c,这array`s指针红宝石?

you mean build an c array and this array`s pointer in ruby?

你可以尝试这样的:

free = Fiddle::Function.new(Fiddle::RUBY_FREE, [TYPE_VOIDP], TYPE_VOID) p = Pointer.malloc(SIZEOF_DOUBLE*len, free)

和调用是这样的:

my_func(p,len)

红宝石GC将调用free函数时,该内存块将不会被使用。

ruby GC will call the free function when this memory block will not be used.

更多推荐

如何应对红宝石小提琴指针数组

本文发布于:2023-07-18 08:20:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142787.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:红宝石   数组   小提琴   指针   如何应对

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!