Dynamic 2D array; program crashes after input

Hi. First time user here. I'm practicing using dynamic 2D arrays. I'm also using classes, so the matrix is a member of the class. I have code to allocate memory for a matrix and code to have the user initialize the matrix from the keyboard.

After the user is prompted to enter values, you can enter all the values needed but after the last row is filled out, the program crashes. The program is crashing before the function 'fillMatrix()' finishes, because I wrote a line of code to print something to the screen at the end of the function, just to try and pinpoint where the program was crashing, and it was not printed to the screen.

Anyone see anything wrong with my code? Thanks in advance!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void matrixType::allocateMatrix()
{
	matrixData = new int* [numRows];
	
	for (int x = 0; x < numRows; x++)
		matrixData[x] = new int[numColumns];
}

void matrixType::fillMatrix()
{
	allocateMatrix();

	cout << "Enter values for the " << numRows << "x" << numColumns << "matrix:\n";
	for (int row = 1; row <= numRows; row++)
	{
		for (int column = 1; column <= numColumns; column++)
			cin >> matrixData[row][column];
	}
}

The for loop on line 14 is supposed to be the same as the loop on line 5 (an array index starts with 0 not 1):

for (int row = 0; row < numRows; row++) // Note: row = 0 and row < numRows

for (int column = 0; column < numColumns; column++)
Thanks coder777. That was a silly mistake on my part. It works now.
Topic archived. No new replies allowed.