tags:

views:

34

answers:

2

Hi all!

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!

+2  A: 

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]

?

Kornel Kisielewicz
+1 And then this is probably trashing the stack, where `board` also lives...
Martin B
@Martin, yes -- a simple assert would immidately find it :)
Kornel Kisielewicz
Solved! I've been looking at wrong variable all the time!
jpou
A: 

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.

Pavel Radzivilovsky