Strange bug - help needed

Hello,
I'm working on a simple program that will be checking if Sudoku is correctly solved.
Here's a part of the program that I have problem with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#define END 9
#define CORRECT_SUM 45

bool checkRows(int *pTab);

int main()
{
	static int sudokuCorrect[9][9] =
	{
		{ 4, 6, 7, 3, 1, 5, 9, 2, 8 },
		{ 1, 8, 2, 9, 4, 6, 7, 5, 3 },
		{ 5, 3, 9, 2, 7, 8, 1, 4, 6 },
		{ 6, 5, 3, 4, 8, 7, 2, 9, 1 },
		{ 2, 7, 8, 1, 9, 3, 4, 6, 5 },
		{ 9, 4, 1, 5, 6, 2, 8, 3, 7 },
		{ 7, 9, 6, 8, 5, 4, 3, 1, 2 },
		{ 8, 2, 4, 6, 3, 1, 5, 7, 9 },
		{ 3, 1, 5, 7, 2, 9, 6, 8, 4 }
	};
	
	int *pSudoku = &sudokuCorrect[0][0];
	
	if (checkRows(pSudoku) == true)
		std::cout << "Sudoku correct!\n";
	else
		std::cout << "Sudoku incorrect :(\n";
	
	return 0;
}

bool checkRows(int *pTab)
{
	for (int i = 0; i < END; i++)
	{
		pTab += (i * END);
		int temp = 0;
		for (int j = 0; j < END; j++)
			temp += *(pTab + j);
			
		if (temp == CORRECT_SUM)
			std::cout << "Row " << i + 1 << " correct.\n";
		else
			std::cout << "Row " << i + 1 << " incorrect.\ttemp = " << temp << std::endl;

		if (temp != CORRECT_SUM)
			return false;
	}

	return true;
}


As you can see checkRows() is checking if sum of every row is equal to 45 (because 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45).
Everything is fine for first 4 lines and then, on line 5, I have output:

1
2
3
4
5
Row 1 correct.
Row 2 correct.
Row 3 correct.
Row 4 correct.
Row 5 incorrect.   temp = 4675980

On the other computer I have similar output:
1
2
3
4
5
Row 1 correct.
Row 2 correct.
Row 3 correct.
Row 4 correct.
Row 5 incorrect.   temp = -1


I tried to debug it and it seems that when loop is counting row 5 something strange happens and variable temp starts to have strange values. But I don't understand why :(
Could anyone please help me? Because I really have no idea what I'm doing wrong :(
pTab += (i * END);
First time you encounter this i is 0, so pTab still points to row 0
Second time i is 1, so now pTab points to row 1.
Third time i is 2, pTab skips 2 rows now and pointing to row 3
Fourth time i is 3, pTab skips even more and pointing to row 6
Fifth time i is 4, pTab points to nonexisting row 10. Now you are in UB field so anything can happen.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
using namespace std;

#define END 9
#define CORRECT_SUM 45

bool checkRows(int pTab[9][9]);

int main()
{
	static int sudokuCorrect[9][9] =
	{
		{ 4, 6, 7, 3, 1, 5, 9, 2, 8 },
		{ 1, 8, 2, 9, 4, 6, 7, 5, 3 },
		{ 5, 3, 9, 2, 7, 8, 1, 4, 6 },
		{ 6, 5, 3, 4, 8, 7, 2, 9, 1 },
		{ 2, 7, 8, 1, 9, 3, 4, 6, 5 },
		{ 9, 4, 1, 5, 6, 2, 8, 3, 7 },
		{ 7, 9, 6, 8, 5, 4, 3, 1, 2 },
		{ 8, 2, 4, 6, 3, 1, 5, 7, 9 },
		{ 3, 1, 5, 7, 2, 9, 6, 8, 4 }
	};
		
	if (checkRows(sudokuCorrect) == true)
		std::cout << "Sudoku correct!\n";
	else
		std::cout << "Sudoku incorrect :(\n";

	int x;
	cin >> x;
	return 0;
}

bool checkRows(int pTab[9][9])
{
	// ROW
	for (int i = 0; i < END; i++)
	{		
		int temp = 0;
		
		// COLUMN
		for (int j = 0; j < END; j++)
			temp += pTab[i][j];

		if (temp == CORRECT_SUM)
			std::cout << "Row " << i + 1 << " correct.\n";
		else
			std::cout << "Row " << i + 1 << " incorrect.\ttemp = " << temp << std::endl;

		if (temp != CORRECT_SUM)
			return false;
	}

	return true;
}


you are trying to access area that is out of bounds so it is outputting garbage.

http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
Man, that was quick! So obvious bug and I just could't find it! Thank you so much!

For anyone interested -- I've changed line:

pTab += (i * END);
to
pTab += END;

and moved it to line 47.
Works like a charm! :)
Topic archived. No new replies allowed.