Announcement

Collapse
No announcement yet.

C help needed

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C help needed

    For some reason, this code simply isn't working.

    Code:
    #include
    #include
    #include
    
    /* 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
    
    The errors it is giving:

    Code:
    *** 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)
    I've not been able to make head or tail of it.

    EDIT: For some reason, the "code" markers don't play nice with C. So I'll attach the file in the next post.

  • #2
    Try finishing that last line of code and closing your braces

    And fix the includes?

    edit: oh, the carets must be screwing stuff up.

    Comment


    • #3
      Originally posted by Kuciwalker
      Try finishing that last line of code and closing your braces

      And fix the includes?
      Attached Files

      Comment


      • #4
        Can't even upload a damn C file.

        Comment


        • #5
          Do you know where in the code the error occurs? (Which free?)

          Comment


          • #6
            Oh, and after a couple minutes I have no idea what's wrong.

            Comment


            • #7
              No. I'll try to find out.

              Comment


              • #8
                Originally posted by Kuciwalker
                Oh, and after a couple minutes I have no idea what's wrong.
                As far as I can make out, the thing should work fine.

                Comment


                • #9
                  A quick GDB session tells me that it's breaking the first time free is called within the determinant function.

                  Comment


                  • #10
                    You crazy Indian, you are writing to memory that isn't yours!

                    This offset:

                    (n-1)*l + m

                    Is going to be larger than the

                    (n-1)*(n-1)

                    you are allocating.

                    You can solve this much more elegantly without pointers BTW.

                    Comment


                    • #11
                      OK, thanks. I got it. I forgot to initialise l and m to 0 for each iteration of the i loop.

                      *Sigh*

                      It's always, ALWAYS the silly, trivial, moronic mistakes that make the for worst debugging nightmares.

                      Comment


                      • #12
                        OK, I happen to have a Borland compiler around here, I'll compile this for you to see if I was right...

                        Comment


                        • #13
                          And what's the method without pointers?

                          Comment


                          • #14
                            Originally posted by VetLegion
                            OK, I happen to have a Borland compiler around here, I'll compile this for you to see if I was right...
                            I just checked.

                            It works.

                            And it compiles fine with gcc, you don't really need a Borland compiler.

                            Comment


                            • #15
                              Well the natural way would be array, saving you some calculation and being easier to read and slightly harder to mess up. I have to run, maybe I'll come back to this later and we can discuss your code

                              Comment

                              Working...
                              X