加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将ruby数组值传递到C数组中

发布时间:2020-12-17 02:59:28 所属栏目:百科 来源:网络整理
导读:我试图在基于 this recipe的C中为ruby制作一个独立的FFT扩展 我已经注意到几种在ruby和c之间传递不同值的方法.然而,对于ruby和C来说,它都是相当新的,并且无法解决如何将数组从VALUE ruby??对象复制到C数组中的问题. 编译错误: SimpleFFT.c:47:错误:下标
我试图在基于 this recipe的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;
}

提前致谢 :)

解决方法

你不能下标inputArr,因为它是一个VALUE而不是一个C数组.即,它是一种标量类型.要访问特定索引,请使用

rb_ary_entry(inputArr,i)

顺便说一句,你可能想首先验证它是一个数组:

Check_Type(rarray,T_ARRAY);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读