Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible segmentation fault: Am I using the "this->" operator correctly?

Tags:

I am doing a homework 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!

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.

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.

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:

void TicTacToe::makeAutoMove(){     srand(time(NULL));     int row = rand() % 3 + 1;     int col = rand() % 3 + 1;     if(this->isValidMove(row, col)){         this->makeMove(row, col);     }else{         this->makeAutoMove();     } } 

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:

char board[4][4]; 

and when I print it, it looks like:

   1  2  3 1  -  -  -  2  -  -  - 3  -  -  - 

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.

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?

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 LEARNING, which means that I am going to have some silly questions sometimes.

Here is more of my .cpp file if it helps:

TicTacToe::TicTacToe() {     for(int row = 0; row < kNumRows; row++){         for(int col = 0; col < kNumCols; col++){             if(col == 0 && row == 0){                 board[row][col] = ' ';             }else if(col == 0){                 board[row][col] = static_cast<char>('0' + row);             }else if(row == 0){                 board[row][col] = static_cast<char>('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 && board[3][3] == middle && middle != '-'){         return middle;     }else if(middle == board[3][1] && middle == board[1][3] && middle != '-'){         return middle;     }      //Check for horizontal wins     for(int row = 1; row < kNumRows; row++){         if(board[row][1] == board[row][2] && board[row][2] == board[row][3] && board[row][1] != '-'){             return board[row][1];         }     }      //Check for vertical wins     for(int col = 1; col < kNumCols; col++){         if(board[1][col] == board[2][col] && board[2][col] == board[3][col] && 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 < kNumCols; col++){         for(int row = 1; row < 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 && board[3][3] == middle && middle != '-'){             return true;         }else if(middle == board[3][1] && middle == board[1][3] && middle != '-'){             return true;         }          //Check for horizontal wins         for(int row = 1; row < kNumRows; row++){             if(board[row][1] == board[row][2] && board[row][2] == board[row][3] && board[row][1] != '-'){                 return true;             }         }          //Check for vertical wins         for(int col = 1; col < kNumCols; col++){             if(board[1][col] == board[2][col] && board[2][col] == board[3][col] && 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] == '-' && row <= 3 && col <= 3){         return true;     }else{         //cout << "That is an invalid move" << endl;         return false;     } }  void TicTacToe::print(){     for(int row = 0; row < kNumRows; row++){         for(int col = 0; col < kNumCols; col++){             cout << setw(3) << board[row][col];         }         cout << endl;      } } 
like image 325
shermanzach Avatar asked Jan 13 '14 19:01

shermanzach


People also ask

How do I find out what is causing my segmentation fault?

Check shell limits Usually it is the limit on stack size that causes this kind of problem. To check memory limits, use the ulimit command in bash or ksh , or the limit command in csh or tcsh . Try setting the stacksize higher, and then re-run your program to see if the segfault goes away.

Why do I get segmentation fault in C++?

A segmentation fault occurs when your program attempts to access an area of memory that it is not allowed to access. In other words, when your program tries to access memory that is beyond the limits that the operating system allocated for your program. Used to being properly initialized.

How do you avoid segmentation fault in CPP?

You have to check that no_prod is < 1024 before writing to it, otherwise you'll write in unallocated memory, which is what gives you a segmentation fault. Once no_prod reached 1024 you have to abort the program (I assume you haven't worked with dynamic allocation yet).

What does the segmentation fault mean?

The segmentation fault means that you try to access a variable, which is not saved. please refer to: im facing a problem while trying to open ansys fluent setup. whenever i try to open it, it takes a long time to load while the software looks like this in the 1st picture. after that, it shows this error message in the 2nd picture. plz help!

How to solve this error - received a fatal signal (segmentation fault)?

Error type: "Error: received a fatal signal (Segmentation fault). Solution: After reading your case file, initialize your case. Then read data file. This will solve your problem. Please feel free to email me if you find something that suits your interests and/or have further questions.

What is segmentation fault in ANSYS FLUENT?

Please feel free to email me if you find something that suits your interests and/or have further questions. The segmentation fault means that you try to access a variable, which is not saved. please refer to: im facing a problem while trying to open ansys fluent setup.

What is the default assignment operator for a class?

Unlike other operators, the compiler will provide a default public assignment operator for your class if you do not provide one. This assignment operator does memberwise assignment (which is essentially the same as the memberwise initialization that default copy constructors do).


1 Answers

A general preface: you almost never need to use this explicitly. In a member function, in order to refer to member variables or member methods, you simply name the variable or method. As with:

class Foo {   int mN; public:   int getIt()   {     return mN; // this->mN legal but not needed   } }; 

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?

this is a pointer, yes. (Actually, it's a keyword.) If you call a non-static member function of a class, this points to the object. For instance, if we were to call getIt() above:

int main() {   Foo f;   int a = f.getIt(); } 

then this would point to f from main().

Static member functions do not have a this pointer. this cannot be NULL, and you cannot change the value of this.

There are several cases in C++ where using this is one way to solve a problem, and other cases where this must be used. See this post for a list of these situations.

like image 96
John Dibling Avatar answered Jan 01 '23 22:01

John Dibling