#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
double** findminor(double** a, int n, int flag) {
double** minor = malloc((n - 1) * sizeof(double*));
int r = 1, c = 0;
for (int i = 0; i < (n - 1); i++) {
*(minor + i) = malloc((n - 1) * sizeof(double*));
for (int j = 0; j < (n - 1); j++) {
if (c != flag) {
*(*(minor +i ) + j) = *(*(a + r) + c);
}
else {
c++;
*(*(minor+i)+j)=*(*(a+r)+c);
}
c++;
}
r++;
}
return minor;
}
void freeMinor(double** minor, int n) {
for (int i = 0; i < (n - 1); i++) {
free(*(minor + i));
}
free(minor);
}
double compute_det(double** a, int n) {
if (n == 1) {
return (double)**a;
}
double result = 0.0;
double sign = 1.0;
double** minor = malloc(n * sizeof(double*));
for (int i = 0; i < n; i++) {
minor = findminor(a, n, i);
result += sign * *(*(a) + i) * compute_det(minor, n - 1 - i);
sign = -1.0;
freeMinor(minor, n);
}
return abs(result);
}
void freeMatrix(double** a, int n) {
for (int i = 0; i < n; i++) {
free(*(a + i));
}
free(a);
}
int main(void) {
int n;
printf("Input dimensions: ");
scanf("%d", &n);
double** a = malloc(n * sizeof(double*));
for (int i = 0; i < n; i++) {
*(a + i) = malloc(n * sizeof(double));
for (int j = 0; j < n; j++) {
scanf("%lf ", (*(a + i) + j));
}
}
printf("%.lf\n", compute_det(a, n));
freeMatrix(a, n);
return 0;
}
Hey, so I'm trying to code a program that computes a determinant for any size. I'm not sure where I'm going wrong because when I pass in these values, which are in a .in file I'm getting 15 instead of 105
4
1 2 3 4
5 2 3 1
2 5 0 1
3 2 4 2
However, I'm also getting a memory leak. Is this what makes my code face errors?
==28233== Memcheck, a memory error detector
==28233== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==28233== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==28233== Command: ./det
==28233==
==28233== Invalid read of size 8
==28233== at 0x10929B: findminor (in /home/u6480784/a3/det/det)
==28233== by 0x1093FE: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x1095E3: main (in /home/u6480784/a3/det/det)
==28233== Address 0x4a4c800 is 0 bytes after a block of size 32 alloc'd
==28233== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==28233== by 0x109572: main (in /home/u6480784/a3/det/det)
==28233==
==28233== Invalid read of size 8
==28233== at 0x10929B: findminor (in /home/u6480784/a3/det/det)
==28233== by 0x1093FE: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x10943D: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x1095E3: main (in /home/u6480784/a3/det/det)
==28233== Address 0x4a4ca38 is 0 bytes after a block of size 24 alloc'd
==28233== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==28233== by 0x10923B: findminor (in /home/u6480784/a3/det/det)
==28233== by 0x1093FE: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x1095E3: main (in /home/u6480784/a3/det/det)
==28233==
==28233== Invalid read of size 8
==28233== at 0x1092F1: findminor (in /home/u6480784/a3/det/det)
==28233== by 0x1093FE: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x10943D: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x1095E3: main (in /home/u6480784/a3/det/det)
==28233== Address 0x4a4ca38 is 0 bytes after a block of size 24 alloc'd
==28233== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==28233== by 0x10923B: findminor (in /home/u6480784/a3/det/det)
==28233== by 0x1093FE: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x1095E3: main (in /home/u6480784/a3/det/det)
==28233==
==28233== Invalid read of size 8
==28233== at 0x1092F1: findminor (in /home/u6480784/a3/det/det)
==28233== by 0x1093FE: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x1095E3: main (in /home/u6480784/a3/det/det)
==28233== Address 0x4a4c800 is 0 bytes after a block of size 32 alloc'd
==28233== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==28233== by 0x109572: main (in /home/u6480784/a3/det/det)
==28233==
Input dimensions: 15
==28233==
==28233== HEAP SUMMARY:
==28233== in use at exit: 88 bytes in 8 blocks
==28233== total heap usage: 48 allocs, 40 frees, 2,376 bytes allocated
==28233==
==28233== 0 bytes in 1 blocks are definitely lost in loss record 1 of 4
==28233== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==28233== by 0x1093DA: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x10943D: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x10943D: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x10943D: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x1095E3: main (in /home/u6480784/a3/det/det)
==28233==
==28233== 16 bytes in 3 blocks are definitely lost in loss record 2 of 4
==28233== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==28233== by 0x1093DA: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x10943D: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x10943D: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x1095E3: main (in /home/u6480784/a3/det/det)
==28233==
==28233== 32 bytes in 1 blocks are definitely lost in loss record 3 of 4
==28233== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==28233== by 0x1093DA: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x1095E3: main (in /home/u6480784/a3/det/det)
==28233==
==28233== 40 bytes in 3 blocks are definitely lost in loss record 4 of 4
==28233== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==28233== by 0x1093DA: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x10943D: compute_det (in /home/u6480784/a3/det/det)
==28233== by 0x1095E3: main (in /home/u6480784/a3/det/det)
==28233==
==28233== LEAK SUMMARY:
==28233== definitely lost: 88 bytes in 8 blocks
==28233== indirectly lost: 0 bytes in 0 blocks
==28233== possibly lost: 0 bytes in 0 blocks
==28233== still reachable: 0 bytes in 0 blocks
==28233== suppressed: 0 bytes in 0 blocks
==28233==
==28233== For lists of detected and suppressed errors, rerun with: -s
==28233== ERROR SUMMARY: 34 errors from 8 contexts (suppressed: 0 from 0)
Is there anything I could do?