我的代码编译但抛出以下异常:
An unhandled exception of type 'System, Access Violation Exception' occured Additional Information: Attempted to read or write protected memory. . .
该错误与s=s+a[z][r]*b[f][h]
这里是代码的副本:
#include"stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int **a, **b;
int z, r, f, h, a_r, a_c, b_r, b_c, s = 0;
cout << "Enter the size of the matrix(nxm) :" << endl;
cin >> a_r >> a_c;
cout << "enter the size of the mask :" << endl;
cin >> b_r >> b_c;
a = (int **) malloc(10 * a_r);
for (int i = 0; i < a_c; i++)
{
a[i] = (int *) malloc(10 * a_c);
}
b = (int **) malloc(10 * b_r);
for (int i = 0; i < b_c; i++)
{
b[i] = (int *) malloc(10 * b_c);
}
for (int i = 0; i < a_r; i++)
{
for (int j = 0; j < a_c; j++)
{
a[i][j] = i + j;
}
}
for (int i = 0; i < b_r; i++)
{
for (int j = 0; j < b_c; j++)
{
b[i][j] = i + j;
}
}
int k = 1, d = 1;
for (int i = 0; i < a_r; i++)
{
for (int j = 0; j < a_c; j++)
{
for (int n = -1; n <= 1; n++)
{
for (int e = -1; e <= 1; e++)
{
z = i + n;
r = j + e;
f = k + n;
h = d + e;
if (z < 0 || z > a_r || r < 0 || r > a_c)
{
s = s + 0;
} else {
s = s + a[z][r] * b[f][h]; // runtime error occurs here
}
}
}
a[i][j] = s;
s = 0;
}
}
return 0;
}