c - segmentation fault with a game of life function -


i'm getting segmentation fault in function , have no idea why. can't isolate problem gdb, tells me function fails , i'm looking see if sees wrong. i'm trying learn c , optimize function. it's function cell evolution implementations of conway's game of life.

i call 2 other functions within 1 return number of neighbors cell has. board 2d array, of width , height.

thanks in advance.

here function:

void evolvecell(board prev, board next) {   int i, j, n, mask;    int width = width;    int height = height;   (i=1; < width-1; ++i) {       (j = 1; j < height-1; ++j) {          n = _neighbors(prev, i, j);          mask = (prev[i][j] << 1);          next[i][j] = !(((n >> prev[i][j]) ^ 3) ^ mask);       }    }     (i = 0; < width; i+= width-1) {      prev[i][j] = prev[i][0];      next[i][j] = next[i][0];      (j = 0; j < height; ++j) {        n = neighbors(prev, i, j);        mask = (prev[i][j] << 1);        next[i][j] = !(((n >> prev[i][j]) ^ 3) ^ mask);      }    }    (j = 0; j < height; j += height-1) {      prev[i][j] = prev[0][j];      next[i][j] = next[0][j];      (i = 0; < width; ++i) {        n = neighbors(prev, i, j);        mask = (prev[i][j] << 1);        next[i][j] = !(((n >> prev[i][j]) ^ 3) ^ mask);      }    } 

}

i suspect there's issue line

 (j = 0; j < height; j += height-1) {      prev[i][j] = prev[0][j]; 

consider previous loop, value of width = 3. evolves 0, 2, 4. when i becomes 4, loop not used, value of 4 , next loop, 1 mentioned above used , when trying access prev[4][0], there's problem.

with gdb - can backtrace (bt) tells line causing problem. advised compile '-g' option (i assuming gcc) or refer compiler's manual enabling debug symbols. tell line that's causing problem , using variable prints (p i, p j) on gdb prompt you'd able debug actual issue.

hope helps


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -