2010-01-27 72 views
1

敲我的头1小时的中间废弃时,无法找到答案:指针矩阵得到的代码

int *get_possible_moves(int **board, int *w, int *h, int *x, int *y) { 
int movespossible=0; 
int moveslist[2][8]; 
//4 
if(x + 1 <= w-1 && y + 2 <= h - 1) { 
    if(board[*(int *)y + 2][*(int *)x + 1] == 0) { 
    moveslist[movespossible][0] = *(int *)x + 1; 
breakpoint-> moveslist[movespossible][1] = *(int *)y + 2; 
    movespossible++; 
    } 
} 

在一个行标breakpoint->我的**被董事会解除引用。任何线索为何如此?没有在内存中使用这个地方的并发线程。另外,我在这条线上根本不使用**板。

//3 
if(x + 2 <= w - 1 && y + 1 <= h - 1) { 
    if(board[*(int *)y + 1][*(int *)x + 2] == 0) { 
    moveslist[movespossible][0] = *(int *)x + 2; 
    moveslist[movespossible][1] = *(int *)y + 1; 
    movespossible++; 
    } 
} 

此代码完美无缺地工作。

在此先感谢!

+0

为什么所有不必要的(int *)都强制转换? – 2010-01-27 11:21:59

回答

2

您有:

int moveslist[2][8]; 

...但你常引用它:

moveslist[movespossible][0] 
moveslist[movespossible][1] 

...你不应该这样做:

moveslist[0][movespossible] 
moveslist[1][movespossible] 

+0

+1然后这可能是垃圾堆栈,'board'也住在哪里...... – 2010-01-27 11:16:25

+0

@Martin,是 - 一个简单的断言会立刻找到它:) – 2010-01-27 11:17:58

+0

解决!我一直在看错误的变量! – jpou 2010-01-27 11:36:34

0

好吧,明白了。数组越界。

movespossible++完成两次时很快发生。

当数组越界时,它将继续在随机存储器空间中行进,最终撞击板子。