[PLUG] [OT] Problem with gcc

Aneesh Mulye aneesh.mulye at gmail.com
Thu Aug 23 23:50:13 IST 2007


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.

Here is the error:

*** glibc detected *** ./a.out: free(): invalid next size (fast):
0x0000000000601040 ***
======= Backtrace: =========
/lib/libc.so.6[0x2ab41ff97b23]
/lib/libc.so.6(cfree+0x8c)[0x2ab41ff9b26c]
./a.out[0x400877]
./a.out[0x40095a]
/lib/libc.so.6(__libc_start_main+0xf4)[0x2ab41ff458e4]
./a.out[0x4005e9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:04 4014120
  /home/aneeshm/work/se/pl-ass/mat/a.out
00600000-00601000 rw-p 00000000 08:04 4014120
  /home/aneeshm/work/se/pl-ass/mat/a.out
00601000-00622000 rw-p 00601000 00:00 0                                  [heap]
2ab41fa89000-2ab41faa5000 r-xp 00000000 08:04 8880137
  /lib/ld-2.5.so
2ab41faa5000-2ab41faaa000 rw-p 2ab41faa5000 00:00 0
2ab41fca4000-2ab41fca6000 rw-p 0001b000 08:04 8880137
  /lib/ld-2.5.so
2ab41fca6000-2ab41fd27000 r-xp 00000000 08:04 8912908
  /lib/libm-2.5.so
2ab41fd27000-2ab41ff26000 ---p 00081000 08:04 8912908
  /lib/libm-2.5.so
2ab41ff26000-2ab41ff28000 rw-p 00080000 08:04 8912908
  /lib/libm-2.5.so
2ab41ff28000-2ab42006f000 r-xp 00000000 08:04 8880190
  /lib/libc-2.5.so
2ab42006f000-2ab42026f000 ---p 00147000 08:04 8880190
  /lib/libc-2.5.so
2ab42026f000-2ab420272000 r--p 00147000 08:04 8880190
  /lib/libc-2.5.so
2ab420272000-2ab420274000 rw-p 0014a000 08:04 8880190
  /lib/libc-2.5.so
2ab420274000-2ab42027a000 rw-p 2ab420274000 00:00 0
2ab42027a000-2ab420287000 r-xp 00000000 08:04 8880139
  /lib/libgcc_s.so.1
2ab420287000-2ab420487000 ---p 0000d000 08:04 8880139
  /lib/libgcc_s.so.1
2ab420487000-2ab420488000 rw-p 0000d000 08:04 8880139
  /lib/libgcc_s.so.1
2ab424000000-2ab424021000 rw-p 2ab424000000 00:00 0
2ab424021000-2ab428000000 ---p 2ab424021000 00:00 0
7fff8b00c000-7fff8b021000 rw-p 7fff8b00c000 00:00 0                      [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vdso]
Aborted (core dumped)




More information about the Plug-mail mailing list