Unhandled exception

Dec 3, 2018 at 9:51am
I wrote an algorithm for matrix multiplication but I have a compiler error "Unhandled exception at 0x0FE8591C (msvcp140d.dll) in matriscarpim.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD. occurred"
does anyone can help me?
all codes here.

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
58
59
60
61
62
63
64
#include <iostream>
using namespace std;
void matrixMult(int matrix1, int matrix2, int a, int b);
int main() {
	int a, b;
	cout<<"please enter number of rows and columns"<<endl;
	cin >> a;
	cin >> b;

	int **matrix1 = new int *[a];
	matrix1[a] = new int[b];

	int **matrix2 = new int *[a];
	matrix2[a] = new int[b];


	cout << "enter value of matrix one" << endl;
	for (int i = 0; i < a; i++) {
		for (int j = 0; j < b; j++) {
		
			cout<<"matrix["<<i<<"][" <<j<< "] degerini giriniz:";
			cin >> matrix1[i][j];
		}
	}

	cout << "enter value of matrix two" << endl;
	for (int i = 0; i < a; i++) {
		for (int j = 0; j < b; j++) {
			cout << "matrix[" << i << "][" << j << "] degerini giriniz:";
			cin >> matrix2[i][j];
		}
	}
	matrixMult(matrix1[a][b],  matrix2[a][b], a,  b);
	int n;
	cin >> n;
	return 0;
}
void matrixMult(int matrix1, int matrix2, int a, int b) {
	int **mult = new int *[a];
	mult[a] = new int[b];

	
	for (int i = 0; i < a; i++) {
		for (int j = 0; j < b; j++) {
			for (int k = 0; k < a; k++) {
				int **matrix1 = new int *[i];
				matrix1[i] = new int[k];

				int **matrix2 = new int *[k];
				matrix2[k] = new int[j];

				mult[i][j] += matrix1[i][k] * matrix2[k][j];
			}
			 
		}
	}
	
	for (int i = 0; i < a; i++) {
		for (int j = 0; j < b; j++) {
			cout << mult[i][j]<<endl;
		}
	}
}

Last edited on Dec 3, 2018 at 12:44pm
Dec 3, 2018 at 10:16am
This is not correct.
1
2
int **matrix1 = new int *[a];
matrix1[a] = new int[b];

The first line creates an array of a int pointers, OK.
The second line creates one array of b integers assigns the array pointer to matrix1[a] which is out of bounds because the last valid index is a-1 (indexes start at zero).
If you want each pointer in the array to point to its own array of b integers you will have to use a loop and allocate a arrays that you assign to the a array elements.
Last edited on Dec 3, 2018 at 10:16am
Dec 3, 2018 at 10:29am
thank you. actually I want to make a program which asks user to enter size of the matrix. it means I want to create dynamic matrices but it does not work that way.
Dec 3, 2018 at 10:33am
Yes. What I meant was something like this.
1
2
3
4
5
int **matrix1 = new int *[a];
for (int i = 0; i < a; ++i)
{
	matrix1[i] = new int[b];
}
Dec 3, 2018 at 11:03am
thank you so much!
Dec 3, 2018 at 11:09am
but now I have an other error in this line:

matrixMult(matrix1[a][b], matrix2[a][b], a, b);
Last edited on Dec 3, 2018 at 11:10am
Dec 3, 2018 at 11:15am
What are aand b? The sizes of your matrix? Because, if so, matrix1[a][b] and matrix2[a][b] are beyond the end of your matrix; you're attempting to access memory that is being used for something else.
Last edited on Dec 3, 2018 at 11:17am
Dec 3, 2018 at 11:28am
yes, but I didn't find any other ways to call function with matrices.
Dec 3, 2018 at 11:31am
It's because the parameter types are incorrect. int should be int**, same as matrix1 and matrix2.

 
void matrixMult(int** matrix1, int** matrix2, int a, int b) {
Dec 3, 2018 at 12:30pm
thank you. It works but incorrect :(
this is programs output


 please enter number of rows and columns
2
2
enter value of matrix one
enter value of matrix[0][0] :4
enter value of matrix[0][1] :5
enter value of matrix[1][0] :8
enter value of matrix[1][1] :7
enter value of matrix two
enter value of matrix[0][0] :6
enter value of matrix[0][1] :4
enter value of matrix[1][0] :1
enter value of matrix[1][1] :5
-168958497
-168958497
-168958497
-168958497 
Last edited on Dec 3, 2018 at 12:30pm
Dec 3, 2018 at 1:04pm
Go though the matrixMult function and make sure that the logic is correct.
Dec 3, 2018 at 1:07pm
@bsr, please post your latest code BELOW THIS POST - full compileable version. There is no way anyone can offer advice on matrix multiplication without seeing it. Your original code was multiplying things that hadn't even been initialised.
Dec 3, 2018 at 1:54pm
this is my whole code. I couldn't see any problems in my matrixMult function.


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
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;
void matrixMult(int** matrix1, int** matrix2, int a, int b);
int main() {
	int a, b;
	cout<<"please enter number of rows and columns"<<endl;
	cin >> a;
	cin >> b;

	int **matrix1 = new int *[a];
	for (int i = 0; i < a; ++i)
	{
		matrix1[i] = new int[b];
	}

	int **matrix2 = new int *[a];
	for (int i = 0; i < a; ++i)
	{
		matrix2[i] = new int[b];
	}

	cout << "enter value of matrix one" << endl;
	for (int i = 0; i < a; i++) {
		for (int j = 0; j < b; j++) {
		
			cout<<"enter value of matrix["<<i<<"][" <<j<< "] :";
			cin >> matrix1[i][j];
		}
	}

	cout << "enter value of matrix two" << endl;
	for (int i = 0; i < a; i++) {
		for (int j = 0; j < b; j++) {
			cout << "enter value of matrix[" << i << "][" << j << "] :";
			cin >> matrix2[i][j];
		}
	}
	matrixMult(matrix1,  matrix2, a,  b);
	int n;
	cin >> n;
	return 0;
}
void matrixMult(int** matrix1, int** matrix2, int a, int b) {
	int **mult = new int *[a];
	for (int i = 0; i < a; ++i)
	{
		mult[i] = new int[b];
	}
	
	
	for (int i = 0; i < a; i++) {
		for (int j = 0; j < b; j++) {
			for (int k = 0; k < a; k++) {

				mult[i][j] += matrix1[i][k] * matrix2[k][j];
			}
			 
		}
	}
	
	for (int i = 0; i < a; i++) {
		for (int j = 0; j < b; j++) {
			cout << mult[i][j]<<endl;
		}
	}
}

Dec 3, 2018 at 2:55pm
You never initialize the matrix elements. If you want them to be zero-initialized you can add () or {} after the square brackets when creating the integer arrays.

 
mult[i] = new int[b]{}; // allocates an array of b ints that are initialized to zero. 

Last edited on Dec 3, 2018 at 3:00pm
Dec 3, 2018 at 3:02pm
thank you so much! it works without any mistake.
Topic archived. No new replies allowed.