为什么我的C扩展没有创建唯一的实例?

时间:2011-06-26 15:00:11

标签: c ruby ruby-c-extension

我创建了一个C扩展,其目的是确定一系列卡片是否会产生直线。这个例子可能比显示问题所需要的要复杂一些,但基本上foo应该在其中存储所有被评估的卡片(每个卡片都有一个指定的索引)。所以吧应该有一套完全独立的卡片和索引。但似乎当我开始将卡分配给bar时,它会覆盖foo。我将在下面包含我的代码,以防我的指针出错。

>> require 'ext/straight_count' #=> true                                                                                            

>> foo = EV::StraightCount.new; 
>> foo.evaluate(6,0); 
>> foo.evaluate(5,1); 
>> foo.evaluate(4,2); 
>> foo.evaluate(3,3); 
>> foo.evaluate(3,4); 
>> foo.evaluate(3,5); 
>> foo.evaluate(3,6); 
>> foo.straight_cards
=> []

>> bar = EV::StraightCount.new; 
>> bar.evaluate(11,0); 
>> bar.evaluate(10,1); 
>> bar.evaluate(9,2); 
>> bar.evaluate(8,3); 
>> bar.evaluate(7,4); 
>> bar.evaluate(2,5); 
>> bar.evaluate(2,6); 
>> bar.straight_cards
=> [11, 10, 9, 8, 7]

>> foo.evaluate(3,6); 
>> foo.straight_cards 
=> [11, 10, 9, 8, 7]

.h文件

static int *pCards;
static int *pSortedCards[NUM_CARDS];
static int i, rMadeHandIndex, cCards[NUM_CARDS], begin_straight, end_straight;

VALUE rIsFound = Qfalse;

static VALUE EV, StraightCount;

static void reset_score();
static VALUE found();
static VALUE score();
static VALUE straight_cards();

1 个答案:

答案 0 :(得分:4)

pSortedCards似乎是全球性的。因此,你有共享状态的原因。