我无法将值插入Ruby数组,并在以后检索它们。
我在第一个函数中放置了我尝试过的不同行的结果。结果是:
VALUE rStraightCards;
static VALUE check_for_straight() {
stuff...
if (begin_straight != NOT_FOUND) {
for (i = begin_straight; i >= end_straight; i--) {
// this gives me a segmentation fault when I call straight_cards()
rb_ary_push(rStraightCards, i);
// these lines give me an empty ary when I call straight_cards()
// RARRAY_PTR(rStraightCards)[i] = i;
// RARRAY_PTR(rStraightCards)[INT2NUM(i)] = INT2NUM(i);
}
}
}
VALUE straight_cards() {
return rStraightCards;
}
void Init_straight_count() {
rStraightCards = rb_ary_new2(NUM_CARDS);
}
答案 0 :(得分:2)
rb_ary_push
的两个参数都应该是VALUE
类型,但是你推了int
(可能):
VALUE
rb_ary_push(VALUE ary, VALUE item)
{
rb_ary_modify(ary);
return rb_ary_push_1(ary, item);
}
试试这个:
rb_ary_push(rStraightCards, INT2NUM(i));
我认为值得注意的是VALUE
通常会这样定义:
typedef uintptr_t VALUE;
因此,int-to-pointer转换的常用警告标志不会捕获此类错误。