Buffer Overrun in simple program

Hi, I'm working through a series of C++ mini exercises, and one was the addition of a 2x3 matrix
Here is my code:
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
//===================================================================
// matrix_addition.cpp By Jonathan Lowe -- 07/07/11
//===================================================================
#include <iostream>

using namespace std;

int main()
{
	int a[2][3], b[2][3], c[2][3];
	cout << "A = " << endl;
	cin >> a[1][1] >> a[1][2] >> a[1][3];
	cin >> a[2][1] >> a[2][2] >> a[2][3];
	cout << endl;

	cout << "B = " << endl;
	cin >> b[1][1] >> b[1][2] >> b[1][3];
	cin >> b[2][1] >> b[2][2] >> b[2][3];
	cout << endl;

	for(int i=1; i<=2; i++)
	{
		for(int j=1; j<=3; j++)
		{
			c[i][j] = a[i][j]+b[i][j];
		}
	}

	cout << "A + B =" << endl;
	cout << c[1][1] << " " << c[1][2] << " " << c[1][3] << endl;
	cout << c[2][1] << " " << c[2][2] << " " << c[2][3] << endl << endl;

	system ("pause");
	return 0;
}


The code executes absolutely correctly as I would expect it to, but when I quit the program, I get a buffer overrun error.
I can't understand the where this is coming from and seems unusual that it only comes at quitting the program, even though it all executes fine..

Any help would be appreciated (compiler is VC++ 2010)
Arrays are indexed from 0, not 1.
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
//===================================================================
// matrix_addition.cpp By Jonathan Lowe -- 07/07/11
//===================================================================
#include <iostream>

using namespace std;

int main()
{
	int a[1][2], b[1][2], c[1][2];
	cout << "A = " << endl;
	cin >> a[0][0] >> a[0][1] >> a[0][2];
	cin >> a[1][0] >> a[1][1] >> a[1][2];
	cout << endl;

	cout << "B = " << endl;
	cin >> b[0][0] >> b[0][1] >> b[0][2];
	cin >> b[1][0] >> b[1][1] >> b[1][2];
	cout << endl;

	for(int i=0; i<=1; i++)
	{
		for(int j=0; j<=2; j++)
		{
			c[i][j] = a[i][j]+b[i][j];
		}
	}

	cout << "A + B =" << endl;
	cout << c[0][0] << " " << c[0][1] << " " << c[0][2] << endl;
	cout << c[1][0] << " " << c[1][1] << " " << c[1][2] << endl << endl;

	system ("pause");
	return 0;
}


Yes ok, now I get no buffer overrun error... but I had already tried this
When I start the array at 0, the addition fails. I can't see why this is, but the results at the end of the program are incorrect, wheras in the first example, despite the buffer overrun, the results are correct
Now the sizes of the arrays are wrong.

int x[5] is an array with 5 elements, accessed as x[0], x[1], x[2], x[3], x[4]
many thanks all understood

the array a[2][3] upper limits are a[1][2] then?
Great help thanks
Your compiler should have generated a lot of warnings like:
warning: array subscript is above array bounds

If it didn't, you should the check your warning settings.
Topic archived. No new replies allowed.