Reputation: 595
==3905== ERROR SUMMARY: 14 errors from 2 contexts (suppressed: 2 from 2)
==3905==
==3905== 6 errors in context 1 of 2:
==3905== Invalid write of size 4
==3905== at 0x401BFE: EliminateXr (in /home/suraj/Desktop/project/fm)
==3905== by 0x402040: fm_elim (in /home/suraj/Desktop/project/fm)
==3905== by 0x401395: name_fm (in /home/suraj/Desktop/project/fm)
==3905== by 0x400C38: main (in /home/suraj/Desktop/project/fm)
==3905== Address 0x51fc724 is 36 bytes inside a block of size 39 alloc'd
==3905== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3905== by 0x401064: alloc_matrix (in /home/suraj/Desktop/project/fm)
==3905== by 0x401A59: EliminateXr (in /home/suraj/Desktop/project/fm)
==3905== by 0x402040: fm_elim (in /home/suraj/Desktop/project/fm)
==3905== by 0x401395: name_fm (in /home/suraj/Desktop/project/fm)
==3905== by 0x400C38: main (in /home/suraj/Desktop/project/fm)
==3905==
==3905==
==3905== 8 errors in context 2 of 2:
==3905== Invalid write of size 4
==3905== at 0x401B17: EliminateXr (in /home/suraj/Desktop/project/fm)
==3905== by 0x402040: fm_elim (in /home/suraj/Desktop/project/fm)
==3905== by 0x401395: name_fm (in /home/suraj/Desktop/project/fm)
==3905== by 0x400C38: main (in /home/suraj/Desktop/project/fm)
==3905== Address 0x51fce4c is 12 bytes inside a block of size 15 alloc'd
==3905== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3905== by 0x401064: alloc_matrix (in /home/suraj/Desktop/project/fm)
==3905== by 0x401A59: EliminateXr (in /home/suraj/Desktop/project/fm)
==3905== by 0x402040: fm_elim (in /home/suraj/Desktop/project/fm)
==3905== by 0x401395: name_fm (in /home/suraj/Desktop/project/fm)
==3905== by 0x400C38: main (in /home/suraj/Desktop/project/fm)
==3905==
--3905--
--3905-- used_suppression: 2 dl-hack3-cond-1
==3905==
==3905== ERROR SUMMARY: 14 errors from 2 contexts (suppressed: 2 from 2)
I am getting errors from valgrind.
It says that error is located in EliminateXr but i can't really see the error. EliminateXr:
void EliminateXr(float** t,float* q,float*** tnew,float** qnew,int n1,int n2,int* r,int* s,int sprime){
float** matrix = (float**)alloc_matrix(sprime,(*r)-1, sizeof(float));
float* vec= (float*)malloc(sprime*sizeof(float));
int matrixIndex=0;
int i,k,l;
for(k = 0; k < n1; ++k){
for(l = n1; l < n2; ++l){
for(i=0; i < *r; ++i){
matrix[matrixIndex][i]=t[k][i]-t[l][i];
}
vec[matrixIndex]=q[k]-q[l];
matrixIndex++;
}
}
for(k = n2; k < *s; ++k){
for(i=0; i < *r; ++i){
matrix[matrixIndex][i]=t[k][i];
}
vec[matrixIndex]=q[k];
matrixIndex++;
}
*tnew=matrix;
*qnew=vec;
*r=(*r)-1;
*s=sprime;
}
I am allocating memory for float so i shouldn't get invalid write of size 4. Can anyone explain how i should use this information: Address 0x51fc724 is 36 bytes inside a block of size 39 alloc'd ==3905== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
If we look at my alloc_matrix i have a malloc
s = m * n * block + m * sizeof(void*) + block - 1;
p = malloc(s);
block= size of float.
Where are the error according to valgrind?
Upvotes: 0
Views: 1065
Reputation: 137404
My psychic powers say that at this line
float** matrix = (float**)alloc_matrix(sprime,(*r)-1, sizeof(float));
you are allocating a matrix capable of holding sprime
rows of floats, each row containing *r - 1
columns.
Yet in your inner loop like this
for(i=0; i < *r; ++i){
matrix[matrixIndex][i]=t[k][i]-t[l][i];
}
You are accessing the matrix as though it contains *r
columns. (i
is going from 0
to *r - 1
) Note that the value of *r
is not changed until later at the end of the function.
Hence, you are trying to write past the end of the buffer, and Valgrind rightfully complains.
Upvotes: 5