title: Possible segmentation fault: Am I using the "this->" operator correctly?
body: <p>I am doing a <strong><em>homework</em></strong> problem that I have a question about. If you don't feel comfortable assisting with a homework problem, I should say that my instructor has encouraged us to ask for help on this site when we are completely stumped. Also, I have completed the basic portion of the assignment on my own, and am now doing an optional challenge problem. Anyway, on to the problem!</p>
  
  <p>Being new to OOP and C++ in general, I am having trouble understanding the "this->" operator. We haven't covered it in class, but I have seen it elsewhere and I am sort-of guessing how it is meant to be used.</p>
  
  <p>For the assignment, I have to create a console based Tic-Tac-Toe game. Only the challenge portion of the assignment wants us to create an AI opponent, and we don't get any extra credit for doing the challenge, I just want to know how to do it. I am studying things like minimax and game trees, but for now I just wanted to create a "pick a random, open spot" function.</p>
  
  <p>I have a class called TicTacToe which is basically the entire program. I will post it below with the parts that are relevant to the question, but part that is giving me an error is this subroutine:</p>
  
  <pre><code>void TicTacToe::makeAutoMove(){
      srand(time(NULL));
      int row = rand() % 3 + 1;
      int col = rand() % 3 + 1;
      if(this-&gt;isValidMove(row, col)){
          this-&gt;makeMove(row, col);
      }else{
          this-&gt;makeAutoMove();
      }
  }
  </code></pre>
  
  <p>The only thing that this function is meant to do is make a move on the board, assuming that it is open. The board is set up like:</p>
  
  <pre><code>char board[4][4];
  </code></pre>
  
  <p>and when I print it, it looks like:</p>
  
  <pre><code>   1  2  3
  1  -  -  - 
  2  -  -  -
  3  -  -  -
  </code></pre>
  
  <p>The problem, is that on occasion a move is made by the computer that gives me an error that is difficult to track down because of the random nature of the function. I believe it is a segfault error, but I can't tell because I can't replicate it in my debugger. </p>
  
  <p>I think that the "this->" operator functions as a pointer, and if a pointer is NULL and it is accessed it could give me this problem. Is this correct? Is there a way to fix this?</p>
  
  <p>I understand that this may be a very low-level question to many of the members of the community, but I would appreciate your help as long as it doesn't come with snide remarks about how trivial this is, or how stupid I must be. I'm <strong>LEARNING</strong>, which means that I am going to have some silly questions sometimes.</p>
  
  <p>Here is more of my .cpp file if it helps:</p>
  
  <pre><code>TicTacToe::TicTacToe()
  {
      for(int row = 0; row &lt; kNumRows; row++){
          for(int col = 0; col &lt; kNumCols; col++){
              if(col == 0 &amp;&amp; row == 0){
                  board[row][col] = ' ';
              }else if(col == 0){
                  board[row][col] = static_cast&lt;char&gt;('0' + row);
              }else if(row == 0){
                  board[row][col] = static_cast&lt;char&gt;('0' + col);
              }else{
                  board[row][col] = '-';
              }
          }
      }
      currentPlayer = 'X';
  }
  
  char TicTacToe::getCurrentPlayer(){
      return currentPlayer;
  }
  
  char TicTacToe::getWinner(){
      //Check for diagonals (Only the middle square can do this)
      char middle = board[2][2];
      if(board[1][1] == middle &amp;&amp; board[3][3] == middle &amp;&amp; middle != '-'){
          return middle;
      }else if(middle == board[3][1] &amp;&amp; middle == board[1][3] &amp;&amp; middle != '-'){
          return middle;
      }
  
      //Check for horizontal wins
      for(int row = 1; row &lt; kNumRows; row++){
          if(board[row][1] == board[row][2] &amp;&amp; board[row][2] == board[row][3] &amp;&amp; board[row][1] != '-'){
              return board[row][1];
          }
      }
  
      //Check for vertical wins
      for(int col = 1; col &lt; kNumCols; col++){
          if(board[1][col] == board[2][col] &amp;&amp; board[2][col] == board[3][col] &amp;&amp; board[1][col] != '-'){
              return board[1][col];
          }
      }
  
      //Otherwise, in the case of a tie game, return a dash.
      return '-';
  }
  
  void TicTacToe::makeMove(int row, int col){
      board[row][col] = currentPlayer;
      if(currentPlayer == 'X'){
          currentPlayer = 'O';
      }else if(currentPlayer == 'O'){
          currentPlayer = 'X';
      }
  }
  
  //TODO: Make sure this works after you make the make-move function
  bool TicTacToe::isDone(){
      bool fullBoard = true;
      //First check to see if the board is full
      for(int col = 1; col &lt; kNumCols; col++){
          for(int row = 1; row &lt; kNumRows; row++){
              if(board[row][col] == '-'){
                  fullBoard = false;
              }
          }
      }
  
      //If the board is full, the game is done. Otherwise check for consecutives.
      if(fullBoard){
          return true;
      }else{
          //Check for diagonals (Only the middle square can do this)
          char middle = board[2][2];
          if(board[1][1] == middle &amp;&amp; board[3][3] == middle &amp;&amp; middle != '-'){
              return true;
          }else if(middle == board[3][1] &amp;&amp; middle == board[1][3] &amp;&amp; middle != '-'){
              return true;
          }
  
          //Check for horizontal wins
          for(int row = 1; row &lt; kNumRows; row++){
              if(board[row][1] == board[row][2] &amp;&amp; board[row][2] == board[row][3] &amp;&amp; board[row][1] != '-'){
                  return true;
              }
          }
  
          //Check for vertical wins
          for(int col = 1; col &lt; kNumCols; col++){
              if(board[1][col] == board[2][col] &amp;&amp; board[2][col] == board[3][col] &amp;&amp; board[1][col] != '-'){
                  return true;
              }
          }
      }
      //If all other tests fail, then the game is not done
      return false;
  }
  
  bool TicTacToe::isValidMove(int row, int col){
      if(board[row][col] == '-' &amp;&amp; row &lt;= 3 &amp;&amp; col &lt;= 3){
          return true;
      }else{
          //cout &lt;&lt; "That is an invalid move" &lt;&lt; endl;
          return false;
      }
  }
  
  void TicTacToe::print(){
      for(int row = 0; row &lt; kNumRows; row++){
          for(int col = 0; col &lt; kNumCols; col++){
              cout &lt;&lt; setw(3) &lt;&lt; board[row][col];
          }
          cout &lt;&lt; endl;
  
      }
  }
  </code></pre>