Accessing elements outside of the defined region of an array is Undefined Behavior (UB).
That means it could:
- Allow you to access uninitialized space (what yours is doing hence the random numbers)
- Segfault
- Any number of other random things.
Basically don’t do it.
In fact stop yourself from being able to do it. Replace those arrays with std::vectors
and use the .at()
call.
for example:
std::vector<std::vector<int>> sudoku(9, std::vector<int>(9, 0));
for (int i=0; i<=8; i++) {
for (int j=0; j<=9; j++) {
cout << sudoku.at(i).at(j);
}
cout << endl;
}
Then you will get a thrown runtime exception that explains your problem instead of random integers or segfaults.
0
solved Weird Array Stuff (Array indexes getting values without me setting it)