c++ - value of variable change event when don't do operation on them -


i have code:

#include <iostream> #include<cstdio> #include<conio.h> #include<windows.h> #define boardsize 3 using namespace std;  int board[boardsize][boardsize]; int cnt = 0; int mincount = boardsize*boardsize;  void boardswitch(int x, int y){     for(int = x-1; <= x+1; i++){         for(int j = y-1; j <= y+1; j++){             if((i>=0) && (j>=0)){                 board[i][j] = board[i][j] ^ 1;             }         }     } }  bool ison(){     int i, j;      for(i = 0; < boardsize; i++){         for(j = 0; j < boardsize; j++){             if(!board[i][j]){                 return 0;             }         }     }     return 1; }  void boarddisp(){     for(int = 0; <boardsize; i++){         for(int j = 0; j < boardsize; j++){             cout<<board[i][j]<<" ";         }         cout<<endl;     } }  void turn(int x){     int i, j;      j = x%boardsize;     = (x-j)/boardsize;     if(x == 0){         cnt = 0;     }     for(int k = 0; k <=1; k++){         printf("%d \n", cnt);         cout<<i<<" "<<j<<" cnt = "<<cnt<<endl;         //getch();         if(k == 1){             boardswitch(i, j);  //switch on (i,j) position             //cout<<i<<" "<<j<<" cnt = "<<cnt<<endl;             //cnt++;             //cout<<"switch on "<<i<<" "<<j<<" cnt = " <<cnt<<endl;         }         if(x == boardsize*boardsize-1){ //          if(ison()){ //              mincount = mincount < cnt ? mincount : cnt; //          }         }         else{             turn(x+1);         }     }     boardswitch(i, j);  // return status before switch     //cnt--; }   int main(){     for(int = 0; < boardsize; i++){         for(int j = 0; j < boardsize; j++){             board[i][j] = rand()%2;     }     }     boarddisp();     turn(0);     if(mincount == boardsize*boardsize){         //there no way turn board on         cout<<"there no way turn board on";     }else{         cout<<mincount;     }     return 0; } 

in thought cnt global variable , every function can change it's value. there no function change it's value when run code (by code block, visual c++) sometime print out value 1 cnt variable , somtine 0. see no operation can make change value of cnt it's change. why ?

with code commented have it, turn() keeps calling increasing values of x until turn(8).

at point, gets set '2' , j gets set '2'. then, call boardswitch(2,2).

your problem happens in boardswitch(). for() loop keeps going until both , j equal 3, resulting in write board[3][3]. location doesn't exist, writing off end of variable -- @ point, clobbered. in case, cnt getting overwritten.

the main point there nothing prevent writing off end of array in c/c++. start writing memory isn't thought was, , program begins behave unpredictably.

ultimately need fix logic. 1 thing might try in meantime creating function wrap writes array , perform bounds checking you. example:

    void writetoboard(int i, int j, int value) {         if (i >= boardsize) {             printf("error: attempted write outside array.\n");             return;         } else if(j >= boardsize) {             printf("error: attempted write outside array.\n");             return;         } else {              board[i][j] = value;         }                  } 

then replace writes array calls function.

this catch problem when happens in easier debug way.

wrapping board in object, making object private, , exposing sort of thing member function excellent example of encapsulation.

an better solution use standard library containers thing (efficiently). isn't option, , it's know conceptually how sort of thing works. rule, though, there reason implement own bounds checked array. that's why have standard library.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

android - MPAndroidChart - How to add Annotations or images to the chart -