Access violation

Greetings fellow coders,

I'm dabbling in C++ and while trying to solve this SudokuChecker problem (https://code.google.com/codejam/contest/2929486/dashboard), using the same input as shown in the link, I keep getting the following error:

Exception thrown at 0x0F45939E (ucrtbased.dll) in SUDOKUCHECKER.exe: 0xC0000005: Access violation writing location 0xCDCDCDCD.

I'm coding in VS Community 2019, and it indicates the error is with the following line:
1
2
3
4
5
for (int i = 0; i < nsquared; i++) {
	for(int j = 0 ; j < nsquared; j++) {
		std::scanf("%d", &sudoku[i][j]);
	}
};

I initialize the matrix as such:
1
2
3
int** sudoku = new int*[nsquared];
for (int i = 0; i < n; i++)
	sudoku[i] = new int[nsquared];



I tried using cin >> sudoku[i][j] to get it to work but no luck in there. What should I change? Creating a separate line to scanf the endline (i.e.
std::scanf("%d\n", &sudoku[i][nsquared-1]);) didn't work either.
Last edited on
for (int i = 0; i < n; i++) // why is this n and not nsquared?
Since you created the sudoku array with nsquared elements, you should initialize nsquared of then.
Hey dhayden,

Nice catch! I changed it here and along with parsing for a few other mistakes I managed to make it work. Thanks a lot!
Topic archived. No new replies allowed.