开发者

pointer matrix get dereferenced in the middle of the code

开发者 https://www.devze.com 2022-12-18 01:19 出处:网络
Banging my head for 1 hour, can\'t find the answer: int *get_possible_moves(int **board, int *w, int *h, int *x, int *y) {

Banging my head for 1 hour, can't find the answer:

    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++;
  }
 }

At a line marked breakpoint-> my **board gets dereferenced. Any clues why it is so? There are no concurrent threads that are using this place in memory. Also, I am not using **board at all at this line.

    //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++;
  }
 }

This code works flawlessly.

Thanks in advance!


You have:

int moveslist[2][8];

... yet you often reference it:

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

... shouldn't you do it:

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

?


OKay, got it. Array out of bounds.

It quickly happens when movespossible++ is done twice.

When array gets out of bounds, it goes on to travel in random memory space, eventually hitting board.

0

精彩评论

暂无评论...
验证码 换一张
取 消