Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Malloc Memory corruption in C

Tags:

c

malloc

cuda

gpu

I have a problem using malloc.

I have a function called jacobi_gpu wich is called many times :

int main(int argc, char* argv[]){

    /* ... */

    int totalrot=0;
    while(nrot>0){
        iter++;
        nrot=jacobi_gpu(a,q, tol, dimmat);
        totalrot+=nrot;

        printf("iter =%3d  nrot=%3d\n",iter, nrot);
    }

    /* ... */
}

The parameters a,q,tol and dimmat are correctly initialized. A and Q are 2 square matrices and dimmat is their dimension.

Here is my code :

int jacobi_gpu(double A[], double Q[], double tol, long int dim){
    int nrot, p, q, k, tid;
    double c, s;
    double *mc, *vc;

    printf("jacobi begins \n");

    mc   = (double *)malloc(2 * dim * sizeof(double));
    vc   = (double *)malloc(2 * dim * sizeof(double));

    if( mc == NULL || vc == NULL){
        fprintf(stderr, "pb allocation matricre\n");
        exit(1);
    }

    nrot = 0;

    for(k = 0; k < dim - 1; k++){
        eye(mc, dim);
        eye(vc, dim);

        for(tid = 0; tid < floor(dim /2); tid++){
            p = (tid + k)%(dim - 1);
            if(tid != 0)
                q = (dim - tid + k - 1)%(dim - 1);
            else
                q = dim - 1;

            //printf("p = %d | q = %d\n", p, q);
            if(fabs(A[p + q*dim]) > tol){

                nrot++;
                symschur2(A, dim, p, q, &c, &s);

                mc[2*tid] = p;        vc[2 * tid] = c;
                mc[2*tid + 1] = q;    vc[2*tid + 1] = -s;

                mc[2*tid + 2*(dim - 2*tid) - 2] = p;
                vc[2*tid + 2*(dim - 2*tid)   - 2 ] = s;

                mc[2*tid + 2*(dim - 2*tid) - 1] = q;
                vc[2 * tid + 2*(dim - 2*tid) - 1 ] = c;     
            }
        }

        affiche(mc,dim,2,"Matrice creuse");
        affiche(vc,dim,2,"Valeur creuse");

    }
    printf("end\n");
    free(mc);
    free(vc);
    return nrot;
}

My problem is in the malloc call on the mc variable :

*** glibc detected *** ./jacobi_gpu: double free or corruption (!prev): 0x00000000022944a0 ***
    *** glibc detected *** ./jacobi_gpu: malloc(): memory corruption: 0x0000000002294580 ***

Any advice?

[EDIT]

  • The function eye initializes an identity matrix
  • The function affiche displays the matrix with lines and columns. The first parameter is the matrix, the second is the number of lines and the third one is the number of column.

More explanation

The purpose of the matrix mc is to store the variables p and q. Those variables contains column indices. The purpose of the matrix vc is to store the values contained in those column. For instance, if the first line of the matrix mc is 0 and 5 ( p = 0, q = 5), that means that the values in the matrix vc will be in the column 0 and 5. If the matrix fifth line in the matrix mc is 2 3 ( p = 2, q = 3), that means that the values in the fifth line in vc will be in column 2 and 3.

Hope this time, i am more clear.

Thanks for your help

like image 723
Dimitri Avatar asked Jan 06 '11 15:01

Dimitri


People also ask

What is malloc (): memory corruption?

A memory leak occurs when you have dynamically allocated memory, using malloc() or calloc() that you do not free properly. As a result, this memory is lost and can never be freed, and thus a memory leak occurs.

What is memory corruption in C?

Definition: Memory corruption can be described as the vulnerability that may occur in a computer system when its memory is altered without an explicit assignment. The contents of a memory location are modified due to programming errors which enable attackers to execute an arbitrary code.

How does memory corruption occur?

Memory corruption occurs in a computer program when the contents of a memory location are modified due to programmatic behavior that exceeds the intention of the original programmer or program/language constructs; this is termed as violation of memory safety.

How do I debug memory corruption?

When a stack corruption is detected, one should look at the local variables in the called and calling functions to look for possible sources of memory corruption. Check array and pointer declarations for sources of errors. Sometimes stray corruption of a processors registers might also be due to a stack corruption.


2 Answers

Identity matrices are always square, but mc is not. When you call eye(mc, dim) I suspect that eye treats mc like it is a dim by dim matrix when it is in fact a 2 by dim matrix, and writes to unallocated memory.

like image 195
Null Set Avatar answered Oct 11 '22 14:10

Null Set


You are not allocating enough memory for a square matrix in your call to malloc(). The correct size would be dim squared, not just 2*dim.

This should do the trick:

mc   = (double *)malloc(dim * dim * sizeof(double));
vc   = (double *)malloc(dim * dim * sizeof(double)); 
like image 31
e.James Avatar answered Oct 11 '22 15:10

e.James