c - 试图理解我的代码中的问题
问题描述
我正在尝试执行以下操作:
• 从用户接收 3 个整数:size1、size2、size3。
• 创建一个size1 * size2 矩阵和一个size2 * size3 矩阵。
• 将两个矩阵相乘。
• 打印结果矩阵。
• 释放所有动态内存。
但是在两个矩阵的输入之后,我希望程序显示矩阵的乘法,但它会导致 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]);
}
}
}
}
解决方案
这是修改后的工作代码。
基本上,乘法函数没有返回分配的地址
,并且与三重指针 pMat 不匹配。
此外,在处理错误分配的内存时应该更加小心。
只需将您的代码与我的代码并排比较即可。
我试图添加有用的评论。
#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)
{
// pMat is a pointer to the "whole" matrix and mirrors
// the calling parameter matrix1, e.g. So always use it dereferenced.
// If *pMat is NULL there is nothing to free (at NULL).
//free(*pMat);
printf("*Not enough RAM.\nTerminating.\n");
exit(1);
}
for (i = 0; i < row; i++)
{
(*pMat)[i] = malloc(col * sizeof(int));
//if(i == 1) //to try exception handling code
if ((*pMat)[i] == NULL)
{
int d;
printf("*Not enough RAM.\nTerminating.\n");
//FreeMatrix(pMat, row);
// Your new matrix isn't complete so you should free *pMat here,
// and free (*pMat)[?] with ? from 0 to i-1
for (d = 0; d < i; ++d) free((*pMat)[d]); free(*pMat);
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++)
{
// pMat is a pointer to the "whole" matrix and mirrors
// the calling parameter matrix1, e.g. So always use it dereferenced.
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)
{
// If c is NULL there is nothing to free (at NULL).
//free(c);
printf("*Not enough RAM.\nTerminating.\n");
exit(1);
}
for (i = 0; i < size1; i++) {
c[i] = malloc(size3 * sizeof(int));
//if(i == 1) //to try exception handling code
if (c[i] == NULL)
{
int d;
printf("*Not enough RAM.\nTerminating.\n");
//FreeMatrix(&c, size1);
// Your new matrix isn't complete so you should free c here,
// and free c[?] with ? from 0 to i-1
for (d =0; d < i; ++d) free(c[d]); free(c);
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;
}
推荐阅读
- qt - 无法使用在线安装程序安装 Qt
- java - Java中的继承和数组
- laravel - 如何解决 Vagrant 中的宅基地版本
- google-sheets - 查询后更改/重命名单元格内容
- sas - 如何使用 DO-LOOP 获得越来越多的变量名称?
- scala - 如何在 Google Colab 中安装 Figaro 编程语言?
- django - 更新后使用帖子参数发布后的django重定向
- reactjs - React 测试库 - 渲染没有显示所有的孩子
- zig - 是否可以将 std.builtin.TypeInfo.Error 转换回其错误值?
- php - 我想根据 WooCommerce 中 woocommerce_form_field 的选择框的值更改显示