Thread: "Stack around the variable 'board' was corrupted" error in this code. But, why!?!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Angry "Stack around the variable 'board' was corrupted" error in this code. But, why!?!

    This is a simple Tic Tac Toe game that i'm programming just to review some C++ concepts.

    Code:
     # include <iostream>
    # include <string>
    # include <iomanip>
    
    using namespace std;
    
    
    const int ROWS = 2;
    const int COLS = 2;
    void showBoard ();
    int main () {
    
    	
    
    	string playerOneName = "";
    	string playerTwoName = "";
    
    	cout << "Player 1, enter your name: ";
    	cin >> playerOneName;
    
    	system ("cls");
    
    	cout << "Player 2, enter your name: ";
    	cin >> playerTwoName;
    
    	system ("cls");
    
    		cout << "\a\aPlayer 1 name is -> " << playerOneName << endl;
    		cout << "Player 2 name is -> " << playerTwoName << endl;
    
    		
    		system ("pause");
    
    		showBoard();
    		
    			system ("pause");
    
    	return 0;
    
    }
    
    void showBoard () 
    	
    {
    	int selection = 0;
    	 int rSelection = 0;
    	 int cSelection = 0;
    	cout << "Here is your board!" << endl;
    
    	
    	char board [ROWS][COLS];
    
    	for (int c = 0; c <= ROWS; c++)
    
    	{
    		cout << setw (10);
    		
    		for (int c2 = 0; c2 <= COLS; c2++)
    		{
    			board [c] [c2] = '*';
    			cout << board [c] [c2];
    		}
    		cout << endl;
    	}
    
    	cout << "Press 1 for instructions or 2 for start to play right now!";
    	cout << "1 or 2? -> ";
    	cin >> selection;
    
    	if (selection == 1)
    
    		cout <<"Do nothing" ;
    	else if (selection == 2)
    
    		system ("cls");
    
    	cout << "Enter the number of the Row that you want the ""X"" : ";
    	cin >> rSelection;
    	cout << "Enter the number of the Column that you want the ""X"" : ";
    	cin >> cSelection;
    
    	rSelection -= 1;
    	cSelection -= 1;
    
    	board [rSelection][cSelection] = 'x';
    
    	for (int c = 0; c <= ROWS; c++)
    
    	{
    		cout << setw (10);
    		
    		for (int c2 = 0; c2 <= COLS; c2++)
    		{
    			
    			cout << board [c] [c2];
    		}
    		cout << endl;
    	}
    
    	
    }
    This is just the beginning and this code is not even near of what it should be, I know. HOWEVER, this code suppose to write an "x" in the board, the user indicate the row and the column that he wants. Why then it trows this exception?? -> "Stack around the variable 'board' was corrupted" If I don't break the debugging and I continue, it put the "x" where it should be. I'm missing something?? Please help!!

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    	for (int c = 0; c <= ROWS; c++)
    
    	{
    		cout << setw (10);
    		
    		for (int c2 = 0; c2 <= COLS; c2++)
    You're writing to elements 0, 1 & 2, but only 0 & 1 exist, so you're corrupting the stack.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Thumbs up

    Quote Originally Posted by cpjust View Post
    Code:
    	for (int c = 0; c <= ROWS; c++)
    
    	{
    		cout << setw (10);
    		
    		for (int c2 = 0; c2 <= COLS; c2++)
    You're writing to elements 0, 1 & 2, but only 0 & 1 exist, so you're corrupting the stack.

    SURE, because the NULL operator need a space too. Wao man, I just missed that. Thank you so much.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by SamyCode View Post
    SURE, because the NULL operator need a space too. Wao man, I just missed that. Thank you so much.
    Err what!
    There's no such thing as a NULL operator and the actual problem is nothing to do with any kind of dummy value required at the end.

    There are two problems:
    1. ROWS and COLS shoud both equal 3, not 2.
    2. The for-loop should go up to less than ROWS or COLS, not less than or equal to
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That error message always means that you have corrupted memory on the stack. Normally this happens when you have overrun the bounds of an array on the stack which is exactly what you did.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Question hmmm...

    Quote Originally Posted by iMalc View Post
    Err what!
    There's no such thing as a NULL operator and the actual problem is nothing to do with any kind of dummy value required at the end.

    There are two problems:
    1. ROWS and COLS shoud both equal 3, not 2.
    2. The for-loop should go up to less than ROWS or COLS, not less than or equal to
    Well I don't understand. This two loops suppose to executes the same number of times or not???

    for (int c = 0; c <= 2, c++)

    and

    for (int c = 0; c < 3, c++)

    Sorry but I think is the same thing!! Or i'm missing something again!?!

    PD. I resolved the error of the stack just by increasing 1 to the size of ROWS and COLS. If it is not for the null operator... where was the error then???

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    There's no need for null terminators in integer arrays! The null terminator is a special case for the use of the C string functions.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Red face

    Oh well! I think I got it. There is not such thing as null operator in this case as you said. Is that I declare the ROW size to 2 and that's means that I want an array with two spaces which was 0 & 1. That's the problem. Sorryyyyyy.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by SamyCode View Post
    Well I don't understand. This two loops suppose to executes the same number of times or not???

    for (int c = 0; c <= 2, c++)

    and

    for (int c = 0; c < 3, c++)

    Sorry but I think is the same thing!! Or i'm missing something again!?!
    Well yes, both of those loops loop over the correct range. However, now instead of using hard coded values all over the place, make proper use of a named constant, as a good little programmer would. The following two options loop over the correct range.
    There are logically three rows, so ROWS must of course be assigned 3. Now:
    Code:
    	for (int c = 0; c <= ROWS-1; c++)
    Code:
    	for (int c = 0; c < ROWS; c++)
    Which one would you prefer to type in? Me, I'd go for the shorter one every time!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Thumbs up Is true...

    Quote Originally Posted by iMalc View Post
    Well yes, both of those loops loop over the correct range. However, now instead of using hard coded values all over the place, make proper use of a named constant, as a good little programmer would. The following two options loop over the correct range.
    There are logically three rows, so ROWS must of course be assigned 3. Now:
    Code:
    	for (int c = 0; c <= ROWS-1; c++)
    Code:
    	for (int c = 0; c < ROWS; c++)
    Which one would you prefer to type in? Me, I'd go for the shorter one every time!
    You are right man. I understand, I just don't know what I was thinking. Maybe it was too late at night. Thank you all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM
  3. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM
  4. Shouldn't either HTML or vB code be enabled in the C++ Board?
    By SilentStrike in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-13-2001, 07:30 AM