-1

I'm trying to write a matrix determinant function in C++. However my code doesn't compile and don't know why (i'm using an online c++ compîler, and the error messages iget is "* Error in `/var/www/service/usercode/519646917/a.out': free(): invalid next size (fast): 0x00000000019c1180 * " ). It seems that the problem comes from the Free function.

Please can anyone tell me what's wrong in my code?

Thanks in advance Regards

    #include <iostream>
    #include <cmath>
    #include <fstream>
    #include <vector>
    #include <numeric>
    #include <iterator>
    #include <map>
    #include <string>

    //C++ clang

    using namespace std;

    void Free(double** a, unsigned int n)
    {
        if (a!=NULL)
        {
            for (unsigned int j=0 ; j<n ; j++)
            {
                delete[] a[j];
            }
            delete[] a;
        }
    }

    double mDeterminant(double** a, unsigned int n)
    {
    if (n==1)
    {
        return a[0][0];
    }
    else if(n==2)
    {
        return a[0][0]*a[1][1]-a[0][1]*a[1][0];
    }
    else
    {
        double res=0.0;
        for (unsigned int i=0 ; i<n ; i++)
        {


            double** A=new double*[n-1];
            for (unsigned int j=0 ; j<n-1 ; j++)
            {
                A[j]=new double[n-1];
            }

            for (unsigned int j=1 ; j<n ; j++)
            {
                unsigned int g=0;
                for (unsigned int k=0 ; k<n ; k++)
                {
                    if (k!=i)
                    {
                        A[j-1][g]=a[j][k]; g++;
                    }
                }
            }

            res+=a[0][i]*pow(-1, i)*mDeterminant(A, n-1);
            Free(A,n-1);
        }
        return res;
    }

   }

    int main()
    {
        unsigned int N=4;
        double** a=new double*[N];
        for (unsigned int i=0 ; i<N ; i++)
        {
            a[i]=new double[N];
        }

        for (unsigned int i=0 ; i < N ; i++)
        {
            for (unsigned int j=0 ; j < N ; j++)
            {
                a[i][j]=(1+i)*(2+j-i);
                cout << a[i][j] << ";";
            }
            cout << endl;
        }


        cout << "----------------------------------" << endl;
        cout << "mDeterminant = " << mDeterminant(a, N) << endl;

    }
JasBeck
  • 25
  • 5
  • 1
    Get a compiler where you can read the error messages !!! This wont be the last time you will have to read error messages produced by your compiler. – 463035818_is_not_an_ai Aug 26 '15 at 11:27
  • 1
    You don't need to use `new` and `delete` at all here, just use `vector>` and relax as it handles all the memory for you. – Useless Aug 26 '15 at 11:30
  • It [compiles](http://ideone.com/YSBi0i) but there's a runtime error. It looks like `g` can reach `n-1`, which is out of bounds. (Even if you don't find a message readable, you can probably find it both copyable and pasteable.) – molbdnilo Aug 26 '15 at 11:37
  • i wanted to train myself to memory management as well – JasBeck Aug 26 '15 at 11:50
  • here's the error i get "*** Error in `/var/www/service/usercode/519646917/a.out': free(): invalid next size (fast): 0x00000000019c1180 *** " . And g doesn't reach n-1 cause there's a case where k=i, so it won't get until n-1 – JasBeck Aug 26 '15 at 11:51
  • You should edit your question with relevant information instead of hiding it in comments. – planetmaker Aug 26 '15 at 12:13
  • Ok ;) edited my quesion – JasBeck Aug 26 '15 at 12:26
  • Dear the first thing I noticed was that the array a in main is of size N by 1 not N by N – AbdulRahman AlHamali Aug 26 '15 at 12:27
  • @AbdulRahmanAlHamali Well spotted. – molbdnilo Aug 26 '15 at 12:28
  • "Please can anyone tell me what's wrong in my code?" – `double** A=new double*[n-1];`, `delete[] a`, etc. Should've been a `vector>`. – The Paramagnetic Croissant Aug 26 '15 at 12:37
  • The `j` loop does `n - 1` iterations. The `k` loop inside it does `n` iterations, and you increment `g` in all except one of them. So `g` goes up to `(n - 1) * (n - 1)`. – molbdnilo Aug 26 '15 at 12:43

2 Answers2

0

You have too many errors:

a[i]=new double; should be a[i]=new double[N];. double** A=new double*[n-1]; should be either double** A=new double*[n]; or loops below should be until n-1. A[j]=new double[n-1]; should be either A[j]=new double[n]; or loops below should be until n-1. Here A[j-1][g]=a[j][k]; g++; your g is out of bound of array. You also not freeing a array. I guess it is easier to rewrite your code from scratch, this time think about what you are doing.

ISanych
  • 21,590
  • 4
  • 32
  • 52
  • the A should be of size (n-1)*(n-1) because it's a minor of the matrix a which is of size n*n. For the loop in function mDeterminant, i should loop until n-1 because i'm checking which values to pick from a in order to build my A – JasBeck Aug 26 '15 at 13:05
0

I believe the only error you have (other than the dimensions of array 'a' in main) is with the place where you define g. g should be defined here:

 for (unsigned int j=1 ; j<n ; j++)
 {
      unsigned int g= 0;
      for (unsigned int k=0 ; k<n ; k++)
      {
          if (k!=i)
          {
              A[j-1][g]=a[j][k]; g++;
          }
      }
  }

Regarding the other n-1 things I understand what you meant with them and I think they are correct

AbdulRahman AlHamali
  • 1,851
  • 1
  • 14
  • 18