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 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
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.
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.
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.
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.
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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With