我正在尝试执行以下功能:
•从用户接收3个整数:size1,size2,size3。
•创建一个size1 * size2矩阵和一个size2 * size3矩阵。
•将2个矩阵相乘。
•打印结果矩阵。
•释放所有动态内存。
在输入两个矩阵之后,BUT我希望程序显示矩阵的乘法,但它会在FreeMatrix函数中导致断点,并这样写:在Project8.exe中的0x0F82AC1D(ucrtbased.dll)处引发异常:0xC0000005:访问冲突读取位置0xCCCCCCC4。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
void BuildMatrix(int*** pMat, int row, int col);
void FreeMatrix(int*** matrix, int row);
void PrintMatrix(int** pMat, int row, int col);
int** MultiplyMatrixes(int** a, int** b, int size1, int size2, int size3);
int main() {
int** matrix1 = NULL, ** matrix2 = NULL, ** matrix3 = NULL;
int* newCol = NULL;
int size1, size2, size3, newRow;
printf("-How many rows in the first matrix?: ");
scanf("%d", &size1);
printf("-How many columns in the first matrix and rows in the second matrix?[size2, size3]: ");
scanf("%d", &size2); /*size2 = rows of matrix2.*/
printf("-How many columns in the second matrix?: ");
scanf("%d", &size3);
/*Build both matrices*/
printf("-First matrix input.\n");
BuildMatrix(&matrix1, size1, size2);
PrintMatrix(matrix1, size1, size2);
printf("-Second matrix input.\n");
BuildMatrix(&matrix2, size2, size3);
PrintMatrix(matrix2, size2, size3);
/*Combine the 2 matrices to a new matrix*/
matrix3 = MultiplyMatrixes(matrix1, matrix2, size1, size2, size3);
FreeMatrix(&matrix2, size2); //Free the second matrix
printf("\n-Multiplied matrix: \n");
PrintMatrix(matrix3, size1, size3);
FreeMatrix(&matrix3, size1);
FreeMatrix(&matrix1, size1);
}
void BuildMatrix(int*** pMat, int row, int col)
{
int i, j;
(*pMat) = (int**)malloc(row * sizeof(int*));
if (*pMat == NULL)
{
free(pMat);
printf("*Not enough RAM.\nTerminating.\n");
exit(1);
}
for (i = 0; i < row; i++)
{
(*pMat)[i] = malloc(col * sizeof(int));
if ((*pMat)[i] == NULL) {
printf("*Not enough RAM.\nTerminating.\n");
FreeMatrix(pMat, row);
exit(1);
}
for (j = 0; j < col; j++) {
printf("-Enter %d element in %d row: ", j + 1, i + 1);
scanf("%d", &(*pMat)[i][j]);
}
printf("\n");
}
//FreeMatrix(pMat, row);
}
void FreeMatrix(int*** matrix, int row)
{
for (int i = 0; i < row; i++)
{
free((matrix)[i]);
}
free(matrix);
}
void PrintMatrix(int** pMat, int row, int col)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
printf("%d ", (pMat[i][j]));
}
printf("\n");
}
}
int** MultiplyMatrixes(int** a, int** b, int size1, int size2, int size3)
{
int i, j, k, ** c = NULL;
c = (int**)malloc(size1 * sizeof(int*));
if (c == NULL)
{
free(*c);
printf("*Not enough RAM.\nTerminating.\n");
exit(1);
}
for (i = 0; i < size1; i++) {
c[i] = malloc(size3 * sizeof(int));
if (c[i] == NULL)
{
printf("*Not enough RAM.\nTerminating.\n");
FreeMatrix(&c, size1);
exit(1);
}
for (j = 0; j < size3; j++)
{
c[i][j] = 0;
for (k = 0; k < size2; k++)
{
c[i][j] += (a[i][k] * b[k][j]);
}
}
}
}
当您为BuildMatrix
中的矩阵分配时:
(*pMat) = (int**)malloc(row * sizeof(int*));
for (i = 0; i < row; i++)
{
(*pMat)[i] = malloc(col * sizeof(int));
...
}
因此,在FreeMatrix
中,您的代码应更改为:
void FreeMatrix(int*** matrix, int row)
{
for (int i = 0; i < row; i++)
{
free((*matrix)[i]); // using *matrix instead of matrix
}
free(*matrix); // using *matrix instead of matrix also
}
此代码中有两个问题。第一个问题,这会在FreeMatrix
函数中导致您的异常。您传递了int***
,但没有取消引用第一个指针。
void FreeMatrix(int*** matrix, int row)
{
for (int i = 0; i < row; i++)
{
free((matrix)[i]); // Type inside free is `int**`, not `int*`
}
free(matrix); // Type inside free is `int***`, not `int**`
}
应该是:
void FreeMatrix(int*** matrix, int row)
{
for (int i = 0; i < row; i++)
{
free((*matrix)[i]); // Type inside free is `int**`, not `int*`
}
free(*matrix); // Type inside free is `int***`, not `int**`
}
而且在解决此问题之后,您的代码中的matrix3
值也出现问题。您缺少return
函数中的MultiplyMatrixes
语句,并且未分配matrix3
变量。
应该是:
int** MultiplyMatrixes(int** a, int** b, int size1, int size2, int size3)
{
int i, j, k, ** c = NULL;
c = (int**)malloc(size1 * sizeof(int*));
if (c == NULL)
{
free(*c);
printf("*Not enough RAM.\nTerminating.\n");
exit(1);
}
for (i = 0; i < size1; i++) {
c[i] = malloc(size3 * sizeof(int));
if (c[i] == NULL)
{
printf("*Not enough RAM.\nTerminating.\n");
FreeMatrix(&c, size1);
exit(1);
}
for (j = 0; j < size3; j++)
{
c[i][j] = 0;
for (k = 0; k < size2; k++)
{
c[i][j] += (a[i][k] * b[k][j]);
}
}
}
return c; // <====
}