将 ruby 数组值传递到 C 数组
我正在尝试为 C 中的 ruby 制作一个独立的 FFT 扩展,基于 这个秘籍
我已经注意到了几种在 ruby 和 c 之间传递不同值的方法。然而,我对 ruby 和 C 都相当陌生,无法弄清楚如何将数组从 VALUE ruby 对象复制到 C 数组中。
编译错误: SimpleFFT.c:47: 错误:下标值既不是数组也不是指针
代码:
#include "ruby.h"
#include "fft.c" // the c file I wish to wrap in ruby
VALUE SimpleFFT = Qnil;
void Init_simplefft();
VALUE method_rfft(VALUE self, VALUE anObject);
void Init_simplefft() {
SimpleFFT = rb_define_module("SimpleFFT");
rb_define_method(SimpleFFT, "rfft", method_rfft, 1);
}
VALUE method_rfft(VALUE self, VALUE inputArr) {
int N = RARRAY_LEN(inputArr); // this works :)
// the FFT function takes an array of real and imaginary paired values
double (*x)[2] = malloc(2 * N * sizeof(double));
// and requires as an argument another such array (empty) for the transformed output
double (*X)[2] = malloc(2 * N * sizeof(double));
for(i=0; i<N; i++) {
x[i][0]=NUM2DBL(inputArr[i]); // ***THIS LINE CAUSES THE ERROR***
x[i][1]=0; // setting all the imaginary values to zero for simplicity
}
fft(N, x, X); // the target function call
// this bit should work in principle, dunno if it's optimal
VALUE outputArr = rb_ary_new();
for(i=0; i<N; i++){
rb_ary_push(outputArr, DBL2NUM(X[i][0]));
}
free(x);
free(X);
return outputArr;
}
提前致谢:)
I'm trying to make a standalone FFT extension for ruby in C, based on this recipe
I've noted several methods for passing different values between ruby and c. However im fairly new to both ruby and C and can't work out how to copy an array from a VALUE ruby object into a C array.
The compilation error:
SimpleFFT.c:47: error: subscripted value is neither array nor pointer
And the code:
#include "ruby.h"
#include "fft.c" // the c file I wish to wrap in ruby
VALUE SimpleFFT = Qnil;
void Init_simplefft();
VALUE method_rfft(VALUE self, VALUE anObject);
void Init_simplefft() {
SimpleFFT = rb_define_module("SimpleFFT");
rb_define_method(SimpleFFT, "rfft", method_rfft, 1);
}
VALUE method_rfft(VALUE self, VALUE inputArr) {
int N = RARRAY_LEN(inputArr); // this works :)
// the FFT function takes an array of real and imaginary paired values
double (*x)[2] = malloc(2 * N * sizeof(double));
// and requires as an argument another such array (empty) for the transformed output
double (*X)[2] = malloc(2 * N * sizeof(double));
for(i=0; i<N; i++) {
x[i][0]=NUM2DBL(inputArr[i]); // ***THIS LINE CAUSES THE ERROR***
x[i][1]=0; // setting all the imaginary values to zero for simplicity
}
fft(N, x, X); // the target function call
// this bit should work in principle, dunno if it's optimal
VALUE outputArr = rb_ary_new();
for(i=0; i<N; i++){
rb_ary_push(outputArr, DBL2NUM(X[i][0]));
}
free(x);
free(X);
return outputArr;
}
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法为
inputArr
添加下标,因为它是VALUE
而不是 C 数组。即,它是标量类型。要访问特定索引,请使用顺便说一句,您可能需要首先验证它是否是一个数组:
You can't subscript
inputArr
because it's aVALUE
rather than a C array. Ie, it's a scalar type. To access a particular index, useAs an aside, you might want to verify first that it's an array:
看起来回答这个问题(并仔细检查我的来源)帮助我找到了答案。
将:替换
为:
似乎可以解决问题:)
我仍然想知道这是否是最有效的做事方式,但它确实有效。
looks like answering the question (and double checking my sources) helped me work out the answer.
replacing:
with:
seemed to do the trick :)
I'm still wonder if this is the most efficient way of doing things, but it works.