I'm getting an error in that says:
" stack smashing detected: ./a.out terminated
Segmentation fault (core dumped) "
after the 2D Array prints in the boardInt() function. The array prints fine,
but I'm not sure why it breaks afterwards. I checked for a bounds issue in the for loop, but I don't see a problem with them.
Your game board is a C-style 2D array, you can't run-time resize it, the dimensions are set at compile time.
You can create a 2D vector array that can be dynamically created. std::vector<std::vector<Spaces>> gameBoard(rowSize, std::vector<Spaces>(COUMN_SIZE));
You create it after you get your row size. You also need to rewrite your BoardInt() function to work with a 2D vector, passed as a reference. A templated function works well.
is there any way I can set each position to the enumerated type BLANK and then set it to character - instead of 1
Changing to a character enum is easy-peasy: enum Spaces { BLANK = '-', RED = 'R', BLUE = 'B' };
Then change how you create the 2D vector: std::vector<std::vector<char>> gameBoard(rowSize, std::vector<char>(COLUMN_SIZE));
Nothing else needs be changed in your code:
-----------------------------------------------------------------------
Input game info
-----------------------------------------------------------------------
How many rows do you want on the board? (4-6 inclusively): 4
Initializing game board...
- - - - - -
- - - - - -
- - - - - -
- - - - - -
Test
You want to change the 2nd row, 5th column to red? (remember arrays and vectors are zero-indexed): gameBoard[1][4] = RED;
I've revamped the code to be more modular, still hacked-ish and crude:
How did you get the stack smashing error? I compiled it under g++ on cygwin and it just compiled with no errors or warnings and ran without any crashes. Input error detection even worked correctly for numbers out of range, although if I put some letters, the error message loops infinitely without allowing new input.
std::cout << "\nInitializing game board...\n";
std::vector<std::vector<char>> gameBoard(rowSize, std::vector<char>(COLUMN_SIZE));
BoardInt(gameBoard);
vs
1 2
std::cout << "\nInitializing game board...\n";
std::vector<std::vector<char>> gameBoard(rowSize, std::vector<char>(COLUMN_SIZE, BLANK));
Edit:
PuTTY is a SSH client. It provides SSH connection to remote machine. Remote machine has SSH server process, some OS, some shell, some compiler, etc. In other words, nothing "runs on PuTTY". I guess your "PC" has Windows and your remote does not.
@zaphraud: Do you use strict standard compliance with g++, or do you allow (the default) GNU extensions? The latter (IMHO unfortunately) supports VLAs.
@zaphraud: Do you use strict standard compliance with g++, or do you allow (the default) GNU extensions? The latter (IMHO unfortunately) supports VLAs.
I haven't turned anything off... so something the OP did is causing a segfault on their machine but it's running without errors or warnings on mine.
...
I added
1 2 3
//print something
cout << "blah de blah";
right before the return 0; statement.
It only prints the blahs with an input value of 4. Otherwise, it silently quits.
If I print values from the array in the same spot, I see the segmentation fault - sometimes.
So yeah, something is still definitely wrong. It's just not giving me as many details about whats going wrong. I was just curious what conditions on what platform lead to a "stack smashing" error.
The stack smashing protector is a compiler-chosen random value that's pushed on the stack right after the return address. That value is checked right before the function returns, and if it has changed, the program has overrun (smashed) the stack.
This is a security feature - it helps prevent malicious users from exploiting buffer overruns.