2010-01-27 139 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++完成兩次時很快發生。

當數組越界時,它將繼續在隨機存儲器空間中行進,最終撞擊板子。