gpt4 book ai didi

c - 在 C 中使用 Malloc 添加两个数组 HW

转载 作者:行者123 更新时间:2023-12-04 10:57:43 24 4
gpt4 key购买 nike

这个程序应该要求用户输入两个矩阵并提供两者的和。编译后它没有按预期工作,我相信这是由于我使用了 malloc,如果有人能提供帮助,我们将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>


/*Here we declare and define our function 'matrices', which prompts the
user for Matrix A and Matrix B and stores their values.*/

int matrices(int rows, int cols){

int i;

int** matrixA = malloc(rows * sizeof(**matrixA));

int** matrixB = malloc(rows * sizeof(**matrixB));

printf("Enter Matrix A\n");

for (i = 0; i < rows; i++){
matrixA[i] = (int *) malloc(cols * sizeof(int));

scanf("%d", matrixA[i]);

}

printf("Enter Matrix B\n");

for (i = 0; i < rows; i++){
matrixB[i] = (int *) malloc(cols * sizeof(int));

scanf("%d", matrixB[i]);

}

return 0;
}

/*Here we declare and define our function 'sum', which sums Matrix A and
Matrix B and provides us with the summation of the two in the
variable 'matrixSum'*/

int sum(int rows, int cols){

int i;

int** matrixA = malloc(rows * sizeof(**matrixA));

int** matrixB = malloc(rows * sizeof(**matrixB));

int** matrixSum = malloc(rows * sizeof(**matrixSum));



printf("A + B =\n");

for (i = 0; i < rows; i++) {

matrixA[i] = (int *) malloc(cols * sizeof(int));
matrixB[i] = (int *) malloc(cols * sizeof(int));
matrixSum[i] = (int *) malloc(cols * sizeof(int));

*matrixSum[i] = *matrixA[i] + *matrixB[i];
printf("%d\t", *matrixSum[i]);


printf("\n");
}

return 0;
}
/*Here we declare and define our main function which works to prompt the user for the number of rows and columns and calls the previously declared functions. */

int main(void){

int rows, cols;

int** matrixA = malloc(rows * sizeof(**matrixA));

int** matrixB = malloc(rows * sizeof(**matrixB));

int** matrixSum = malloc(rows * sizeof(**matrixSum));

printf("Please enter the number of rows: ");
scanf("%d", &rows);
printf("Please enter the number of columns: ");
scanf("%d", &cols);


matrices(rows, cols);
sum(rows, cols);

free(matrixA);
free(matrixB);
free(matrixSum);

return 0;
}

最佳答案

  • int** matrixA; 指向的大小将是 sizeof(*matrixA),而不是 sizeof(**matrixA)。在指针和 int 大小不同的环境中,使用后者会使缓冲区太小。
  • 在函数 matrices() 中,一些输入被读取然后被丢弃。内存泄漏也会发生。
  • 在函数 sum() 中,使用了通过 malloc() 分配且未初始化的缓冲区中的不确定值,并调用了未定义的行为
  • 他们说 you shouldn't cast the result of malloc() in C .

更正:

  • 进行适当的尺寸计算。
  • 避免造成内存泄漏,保留并传递读取的内容。
  • 使用循环读取所有元素,而不仅仅是每行的第一个元素。

这是一个带错误检查的固定代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

void matrices(int rows, int cols, int*** matrixA, int*** matrixB){

int i, j;

*matrixA = malloc(rows * sizeof(**matrixA));
if (*matrixA == NULL){
perror("matrixA malloc");
exit(1);
}

*matrixB = malloc(rows * sizeof(**matrixB));
if (*matrixB == NULL){
perror("matrixB malloc");
exit(1);
}

printf("Enter Matrix A\n");

for (i = 0; i < rows; i++){
(*matrixA)[i] = malloc(cols * sizeof(int));
if ((*matrixA)[i] == NULL){
perror("matrixA[i] malloc");
exit(1);
}

for (j = 0; j < cols; j++){
if (scanf("%d", &(*matrixA)[i][j]) != 1){
fputs("matrixA read error\n", stderr);
exit(1);
}
}

}

printf("Enter Matrix B\n");

for (i = 0; i < rows; i++){
(*matrixB)[i] = malloc(cols * sizeof(int));
if ((*matrixB)[i] == NULL){
perror("matrixB[i] malloc");
exit(1);
}

for (j = 0; j < cols; j++){
if (scanf("%d", &(*matrixB)[i][j]) != 1){
fputs("matrixB read error\n", stderr);
exit(1);
}
}

}

}

void sum(int rows, int cols, int** matrixA, int** matrixB, int*** matrixSum){

int i, j;

*matrixSum = malloc(rows * sizeof(**matrixSum));
if (*matrixSum == NULL){
perror("matrixSum malloc");
exit(1);
}

printf("A + B =\n");

for (i = 0; i < rows; i++) {

(*matrixSum)[i] = malloc(cols * sizeof(int));
if ((*matrixSum)[i] == NULL){
perror("matrixSum[i] malloc");
exit(1);
}

for (j = 0; j < cols; j++){
(*matrixSum)[i][j] = matrixA[i][j] + matrixB[i][j];
printf("%d\t", (*matrixSum)[i][j]);
}

printf("\n");
}

}
/*Here we declare and define our main function which works to prompt the user for the number of rows and columns and calls the previously declared functions. */

int main(void){

int rows, cols;

int i;

int** matrixA;
int** matrixB;
int** matrixSum;

printf("Please enter the number of rows: ");
if (scanf("%d", &rows) != 1){
fputs("rows read error\n", stderr);
return 1;
}
printf("Please enter the number of columns: ");
if (scanf("%d", &cols) != 1){
fputs("cols read error\n", stderr);
return 1;
}

matrices(rows, cols, &matrixA, &matrixB);
sum(rows, cols, matrixA, matrixB, &matrixSum);

for (i = 0; i < rows; i++) {
free(matrixA[i]);
free(matrixB[i]);
free(matrixSum[i]);
}

free(matrixA);
free(matrixB);
free(matrixSum);

return 0;
}

请注意,*matrixA 指向的是 **matrixA,因此现在使用 sizeof(**matrixA) 是正确的。

关于c - 在 C 中使用 Malloc 添加两个数组 HW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37234957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com