Home > Mobile >  malloc(): corrupted top size for matrix inverse
malloc(): corrupted top size for matrix inverse

Time:10-26

I am trying to create a matrix calculator and when I try to use the inverse function I get an error saying "malloc(): corrupted top size". I do not know what this error means and how to fix it.

This is my code:

double** inverse(double *mat[], int n){
    //identity matrix
    double **I = malloc(sizeof(double*) * n);
    for (int i=0; i<n; i  ) I[i] = malloc(sizeof(double) * n);
    for(int i=0; i<n; i  ){
        for(int j=0; j<n; j  ){
            if(i==j){
                I[i][j] = 1;
            }else{
                I[i][j] = 0;
            }
        }
    }
        
    double f = 0.0;
    double sub = 0.0;
    for(int p=0; p<n; n  ){
        f = mat[p][p];
        for(int x=0; x<n; x  ){
            mat[p][x] = mat[p][x] / f;
            I[p][x] = I[p][x] / f; (line 45)
        } 
        for(int i=p 1; i<n; i  ){
            f = mat[i][p]; (line 48)
            for(int x=0; x<n; x  ){
                sub = mat[p][x] * f;
                mat[i][x] = mat[i][x] - sub; (line 51)
                    
                sub = I[p][x] * f;       
                I[i][x] = I[i][x] - sub; (line 54)
            }
        }
    }
        
    for(int p=n-1; p>=0; p--){
        for(int i=p-1; i>=0; i--){
            f = mat[i][p];
            for(int x=0; x<n; x  ){
                sub = mat[p][x] * f;
                mat[i][x] = mat[i][x] - sub;
                    
                sub = I[p][x] * f;
                I[i][x] = I[i][x] - sub;
            }
        }
    }
    //return I;
    printf("I:\n");
    for(int i=0; i<n; i  ){
        for(int j=0; j<n; j  ){
            printf("%f ",I[i][j]);
        }
        printf("\n");
    }
        
    //free
    for (int i=0; i<n; i  ) free(I[i]);
    free(I);
}

void multiply(double *mat1[], double *mat2[], int R1, int C1, int R2, int C2, double *rslt[]) {
    for (int i = 0; i < R1; i  ) {
        for (int j = 0; j < C2; j  ) {
            rslt[i][j] = 0;
 
            for (int k = 0; k < R2; k  ) {
                rslt[i][j]  = mat1[i][k] * mat2[k][j];
            }
            printf("%f\t", rslt[i][j]);
        }
        printf("\n");
    }
    
    //return rslt;
}

int main(){
    double **rslt = malloc(sizeof(double*) * n);
    for (int i=0; i<n; i  ) rslt[i] = malloc(sizeof(double) * (k 1));
    multiply(x,t,n,k 1,k 1,n,rslt);
    /* x and t are matrices, n=7, k 1=5,(these are the matrix dimensions for x, and the opposite are the matrix dimensions for t) */
    /* the variables in the call to multiply(x and t are matrices, n and k 1 are
       matrix dimensions, and rslt stores the result) are declared and cause no errors*/
    /* the code works with no errors upto this point*/

    /* rslt is the resulting matrix after the call to the multiply function
       and n is the matrix dimensions */
    inverse(rslt,n);
}

I am trying to find the inverse after multiplying two matrices. The code uses the "rslt" matrix which holds the product and calls the inverse function. The multiply functions works perfectly and I only get an error after the call to "inverse()".

After running valgrind it shows me that the error occurs in the inverse function. It seems to occur multiple times in the for loops when I try to access the matrices. It says the leaks are in lines 45, 48, 51, and 54(I specified which lines they are above). The specific error valgrind shows me is "Invalid read of size 8" and "Invalid write of size 8".

CodePudding user response:

When you multiply a R1xC1 matrix with a R2xC2 (where C1 must equal R2), you'll get a R1xC2 matrix.

But you don't allocate that for the result.

for (int i=0; i<n; i  ) rslt[i] = malloc(sizeof(double) * (k 1))
multiply(x,t,n,k 1,k 1,n,rslt);                           ^^^^^
             ^         ^                                  wrong
             R1        C2

So here

void multiply(double *mat1[], double *mat2[], int R1, int C1, int R2, int C2, double *rslt[]) {
    for (int i = 0; i < R1; i  ) {
        for (int j = 0; j < C2; j  ) {
            rslt[i][j] = 0;

you write outside the allocated memory (because n is bigger than k 1).

You want to do

for (int i=0; i<n; i  ) rslt[i] = malloc(sizeof(double) * n)
                                                          ^
                                                        notice
  • Related