C 试图理解代码中的问题
我正在尝试执行以下功能: •从用户处接收3个整数:size1、size2、size3 •创建一个size1*size2矩阵和一个size2*size3矩阵 •将2个矩阵相乘 •打印结果矩阵 •释放所有动态内存 但在输入两个矩阵后,我希望程序显示矩阵的乘法,但它会导致FreeMatrix函数出现断点,并这样写: Project8.exe中0x0F82AC1D(ucrtbased.dll)处引发异常:0xC0000005:访问冲突读取位置0xCCC4 守则:C 试图理解代码中的问题,c,visual-studio,matrix,compiler-errors,breakpoints,C,Visual Studio,Matrix,Compiler Errors,Breakpoints,我正在尝试执行以下功能: •从用户处接收3个整数:size1、size2、size3 •创建一个size1*size2矩阵和一个size2*size3矩阵 •将2个矩阵相乘 •打印结果矩阵 •释放所有动态内存 但在输入两个矩阵后,我希望程序显示矩阵的乘法,但它会导致FreeMatrix函数出现断点,并这样写: Project8.exe中0x0F82AC1D(ucrtbased.dll)处引发异常:0xC0000005:访问冲突读取位置0xCCC4 守则: #define _CRT_SECURE_
#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]);
}
}
}
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
无效构建矩阵(int***pMat、int行、int列);
无效自由矩阵(int***矩阵,int行);
无效打印矩阵(整数**pMat,整数行,整数列);
整数**多矩阵(整数**a、整数**b、整数大小1、整数大小2、整数大小3);
int main(){
int**matrix1=NULL,**matrix2=NULL,**matrix3=NULL;
int*newCol=NULL;
int size1、size2、size3、newRow;
printf(“-第一个矩阵中有多少行?”:”;
scanf(“%d”和大小1);
printf(“-第一个矩阵中有多少列,第二个矩阵中有多少行?[size2,size3]:”;
scanf(“%d”,&size2);/*size2=矩阵2的行*/
printf(“-第二个矩阵中有多少列?:”;
scanf(“%d”&size3);
/*建立两个矩阵*/
printf(“-第一个矩阵输入。\n”);
构建矩阵(矩阵1、尺寸1、尺寸2);
打印矩阵(矩阵1、尺寸1、尺寸2);
printf(“-第二个矩阵输入。\n”);
构建矩阵(矩阵2、尺寸2、尺寸3);
打印矩阵(矩阵2、尺寸2、尺寸3);
/*将这两个矩阵组合成一个新矩阵*/
矩阵3=多矩阵(矩阵1、矩阵2、尺寸1、尺寸2、尺寸3);
FreeMatrix(&matrix2,size2);//释放第二个矩阵
printf(“\n-乘法矩阵:\n”);
打印矩阵(矩阵3、尺寸1、尺寸3);
FreeMatrix(&matrix3,尺寸1);
FreeMatrix(&matrix1,尺寸1);
}
无效构建矩阵(整数***pMat,整数行,整数列)
{
int i,j;
(*pMat)=(int**)malloc(row*sizeof(int*);
如果(*pMat==NULL)
{
免费(pMat);
printf(“*内存不足。\n正在终止。\n”);
出口(1);
}
对于(i=0;i|
当您在BuildMatrix
中为矩阵分配时:
(*pMat) = (int**)malloc(row * sizeof(int*));
for (i = 0; i < row; i++)
{
(*pMat)[i] = malloc(col * sizeof(int));
...
}
您还忘记了返回多矩阵
函数的值。它应该在函数末尾返回矩阵c
您不需要释放NULL
指针,因为如果指针为NULL,则不会执行任何操作,例如,在您的代码中:
(*pMat) = (int**)malloc(row * sizeof(int*));
if (*pMat == NULL)
{
free(pMat); // it's not necessary
printf("*Not enough RAM.\nTerminating.\n");
exit(1);
}
这段代码中有两个问题。 第一个问题,导致您在
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**
}
我还建议您将矩阵分配为指针行和连续数组。
像这样:
int** matrix = (int*)malloc(sizeof(int*) * rows); // pointers to rows
matrix[0] = (int*)malloc(rows * cols); // single chunk of memory for all elems
for(int i = 0; i < rows; ++i)
matrix[i] = matrix[0] + i * cols;
int** matrix = (int*)malloc(sizeof(int*) * cols * rows); // single chunk of memory for all elems
释放:
if (rows)
free(matrix[0]);
free(matrix);
另一个选项是将矩阵分配为连续数组
像这样:
int** matrix = (int*)malloc(sizeof(int*) * rows); // pointers to rows
matrix[0] = (int*)malloc(rows * cols); // single chunk of memory for all elems
for(int i = 0; i < rows; ++i)
matrix[i] = matrix[0] + i * cols;
int** matrix = (int*)malloc(sizeof(int*) * cols * rows); // single chunk of memory for all elems
元素访问:
elem = matrix[row_index][col_index];
elem = matrix[row_index * row_size + col_index];
在这种方法中,取消分配更加简单:
if(matrix)
free(matrix);
作为一个简单的内存块分配简化了内存释放,这样的代码对缓存更加友好。下面是修改后的工作代码
基本上,乘法函数没有返回分配的地址
并且与三指针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;
}
#包括
#包括
无效构建矩阵(int***pMat、int行、int列);
无效自由矩阵(int***矩阵,int行);
无效打印矩阵(整数**pMat,整数行,整数列);
整数**多矩阵(整数**a、整数**b、整数大小1、整数大小2、整数大小3);
int main(){
int**matrix1=NULL,**matrix2=NULL,**matrix3=NULL;
int*newCol=NULL;
int size1、size2、size3、newRow;
printf(“-第一个矩阵中有多少行?”:”;
scanf(“%d”和大小1);
printf(“-第一个矩阵中有多少列,第二个矩阵中有多少行?[size2,size3]:”;
scanf(“%d”,&size2);/*size2=矩阵2的行*/
printf(“-第二个矩阵中有多少列?:”;
scanf(“%d”&size3);
/*建立两个矩阵*/
printf(“-第一个矩阵输入。\n”);
构建矩阵(矩阵1、尺寸1、尺寸2);
打印矩阵(矩阵1、尺寸1、尺寸2);
printf(“-第二个矩阵输入。\n”);
构建矩阵(矩阵2、尺寸2、尺寸3);
P
#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;
}