> Message: 1 > Date: Thu, 23 Aug 2007 23:50:13 +0530 > From: "Aneesh Mulye" <[EMAIL PROTECTED]> > Subject: [PLUG] [OT] Problem with gcc > To: "Pune GNU/Linux Users Group Mailing List" <[email protected]> > Message-ID: > <[EMAIL PROTECTED]> > Content-Type: text/plain; charset=ISO-8859-1 > > For some reason, the following code doesn't work: > > #include<stdio.h> > #include<stdlib.h> > #include<math.h> > > /* float determinant(float *a, int n) {{{1 */ > float determinant(float *a, int n) > { > float *b, det=0; > > if(n<2) > return 0; > > if(n==2) > det=( (*a)*(*(a+3)) - (*(a+1))*(*(a+2))); > else > { > b=malloc((n-1)*(n-1)*sizeof(float)); > > int i,j,k,m=0,l=0; > > for(i=0;i<n;i++) > { > for(j=1;j<n;j++) > for(k=0;k<n;k++) > if(k!=i) > { > *(b + (n-1)*l + m)= > *(a + n*j+ k); > m++; > l+=m/(n-1); > m%=(n-1); > } > > det+=(*(a+i))*determinant(b, n-1)*pow(-1,i); > } > > free(b); > } > > return det; > } > /* 1}}} */ > > int main() > { > float *a, det; > int n,i,j; > > printf("Enter the no. of rows\n# "); > scanf("%d", &n); > printf("\n\n"); > > a=malloc(n*n*sizeof(float)); > > for(i=0;i<n;i++) > for(j=0;j<n;j++) > { > printf("Enter element (%d,%d)# ", (i+1), (j+1)); > scanf("%f", (a + i*n + j)); > } > > det=determinant(a, n); > > free(a); > > printf("\n\nThe determinant is: %f\n\n", det); > > return 0; > } > > Any help is appreciated. >
Problem is in line 27 of your code: *(b + (n-1)*l + m) = *(a + n*j+ k); For debugging this, use "valgrind" which will tell you invalid memory writes. Here is the output of debugging: [EMAIL PROTECTED]:/tmp/work$ valgrind ./a.out ==6344== Memcheck, a memory error detector. ==6344== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al. ==6344== Using LibVEX rev 1658, a library for dynamic binary translation. ==6344== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP. ==6344== Using valgrind-3.2.1-Debian, a dynamic binary instrumentation framework. ==6344== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al. ==6344== For more details, rerun with: -v ==6344== Enter the no. of rows # 3 Enter element (1,1)# 1 Enter element (1,2)# 2 Enter element (1,3)# 3 Enter element (2,1)# 4 Enter element (2,2)# 5 Enter element (2,3)# 6 Enter element (3,1)# 7 Enter element (3,2)# 8 Enter element (3,3)# 9 ==6344== Invalid write of size 4 ==6344== at 0x80485AB: determinant (test.c:27) ==6344== by 0x804873D: main (test.c:62) ==6344== Address 0x41A1090 is 0 bytes after a block of size 16 alloc'd ==6344== at 0x4021620: malloc (vg_replace_malloc.c:149) ==6344== by 0x804853C: determinant (test.c:17) ==6344== by 0x804873D: main (test.c:62) The determinant is: -6.000000 ==6344== ==6344== ERROR SUMMARY: 8 errors from 1 contexts (suppressed: 13 from 1) ==6344== malloc/free: in use at exit: 0 bytes in 0 blocks. ==6344== malloc/free: 2 allocs, 2 frees, 52 bytes allocated. ==6344== For counts of detected errors, rerun with: -v ==6344== All heap blocks were freed -- no leaks are possible. Cheers, Ameya. -- ______________________________________________________________________ Pune GNU/Linux Users Group Mailing List: ([email protected]) List Information: http://plug.org.in/cgi-bin/mailman/listinfo/plug-mail Send 'help' to [EMAIL PROTECTED] for mailing instructions.
