This program is supposed to ask the user to enter two matrices and provide the sum of the two. When compiled it does not work as expected, I believe it is due to my use of malloc, if anyone can help this would be greatly appreciated.
#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; will be sizeof(*matrixA), not sizeof(**matrixA). In environments in which size of pointers and int differs, using latter will make the buffer too small.matrices(), some input are read and then discarded. Memory leak also happens.sum(), indeterminate values in buffer allocated via malloc() and not initialized are used and undefined behavior is invoked.malloc() in C.To correct:
Here is a fixed code with error checking:
#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;
}
Note that what is pointed by *matrixA is **matrixA, so using sizeof(**matrixA) is now correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With