简体   繁体   English

为什么我从ruby数组中提取的值到我的c扩展名错了?

[英]Why are the values I am pulling from my ruby array to my c extension wrong?

This method is just verifying that I'm able to see the elements of a ruby array correctly. 这个方法只是验证我能够正确地看到ruby数组的元素。

static VALUE 
print_cards(self) 
  VALUE self;
{
    VALUE cards;
    int i;

    cards = rb_ivar_get(self, rb_intern("@cards"));
    VALUE *ary_ptr = RARRAY_PTR(cards);
    int ary_length = RARRAY_LEN(cards);

    for(i=0; i< ary_length; i++)
        printf("%d\n", ary_ptr[i]);

  return Qnil;
}

void Init_ev() {
    rb_eval_string("require './lib/ev/pair_counter'");
    VALUE PairCounter = rb_path2class("EV::PairCounter");
    rb_define_method(PairCounter, "print_cards", print_cards, 0);
}

But when I put the method to use, the elements of the array are wrong. 但是当我使用该方法时,数组的元素是错误的。 The strange thing is that it doesn't look like I'm getting some kind of address information, since the size of the number that is printed roughly corresponds with the size of the number in the ruby array. 奇怪的是,它看起来并不像我得到某种地址信息,因为打印的数字大小大致与ruby数组中的数字大小相对应。 The numbers are also consistent each time I create a new object and run print_cards. 每次创建新对象并运行print_cards时,数字也是一致的。

ruby-1.9.2-p180 :001 > p = EV::PairCounter.new   #=> #<EV::PairCounter:0x000001046a10f8 @pairs={}, @cards=[]>
ruby-1.9.2-p180 :002 > p.add_card(1)   #=> 1
ruby-1.9.2-p180 :003 > p.print_cards
3                                      #=> nil
ruby-1.9.2-p180 :004 > p.add_card(5)   #=> 2
ruby-1.9.2-p180 :005 > p.add_card(88)   #=> 3
ruby-1.9.2-p180 :006 > p
=> #<EV::PairCounter:0x000001046a10f8 @pairs={1=>1, 5=>1, 88=>1}, @cards=[1, 5, 88]>
ruby-1.9.2-p180 :007 > p.print_cards
3
11
177                 

我需要使用

printf("%d\n", NUM2INT(ary_ptr[i]));

rb_ary_entry is the safe way to get the content from a Ruby array. rb_ary_entry是从Ruby数组中获取内容的安全方法。 They are not accessed like normal C arrays. 它们不像普通的C数组那样被访问。

Seems to be related to this question: https://stackoverflow.com/a/9619163/486990 似乎与这个问题有关: https//stackoverflow.com/a/9619163/486990

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从c扩展名访问ruby数组? - How do I access a ruby array from my c extension? 为什么我的数组在C中返回错误的值? - Why is my array returning the wrong values in C? 为什么我可以直接使用某些 ruby C 扩展数组方法,而不能直接使用其他方法? - Why am I able to directly use some of the ruby C extension array methods, but not others? 在C中为字符数组赋值时,为什么整数数组元素包含错误的值,并且该如何解决? - Why do my integer array elements contain the wrong values when I assign it character values in C and how can I fix this? 为什么我的阵列中出现重复项? - Why am I getting duplicates in my array? 当我尝试将数组中的元素分配给 C 中数据结构中的相同类型值时,为什么会出现分段错误? - Why am I getting a segmentation fault when I try to assign an element from an array to a same type value in my data structure in C? 我写了一个 C 程序来计算数组的标准偏差。 我的代码给出了错误的答案。 我究竟做错了什么? - I wrote a C program to calculate the standard deviation of an array. My code is giving the wrong answer. What am I doing wrong? 如何使用交流电扩展来扩展我的红宝石课程? - How do I extend my ruby class with a c extension? 我在这个 C 扩展中引用我的实例的方式有什么问题吗? - Is there something wrong with how I'm referencing my instances in this C extension? 我的c函数在做什么错 - What am i doing wrong in my c function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM