C语言 C 中的 Malloc 内存损坏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4616503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Malloc Memory corruption in C
提问by Dimitri
I have a problem using malloc.
我在使用 malloc 时遇到问题。
I have a function called jacobi_gpuwich is called many times :
我有一个名为jacobi_gpu至极的函数被多次调用:
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.
参数 a,q,tol 和 dimmat 已正确初始化。A 和 Q 是 2 个方阵,dimmat 是它们的维度。
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 :
我的问题出在对 mc 变量的 malloc 调用中:
*** glibc detected *** ./jacobi_gpu: double free or corruption (!prev): 0x00000000022944a0 ***
*** glibc detected *** ./jacobi_gpu: malloc(): memory corruption: 0x0000000002294580 ***
Any advice?
有什么建议吗?
[EDIT]
[编辑]
- The function eyeinitializes an identity matrix
- The function affichedisplays 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.
- 函数eye初始化一个单位矩阵
- 函数affich显示带有行和列的矩阵。第一个参数是矩阵,第二个是行数,第三个是列数。
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.
矩阵 mc 的目的是存储变量 p 和 q。这些变量包含列索引。矩阵 vc 的目的是存储这些列中包含的值。例如,如果矩阵 mc 的第一行是 0 和 5(p = 0,q = 5),这意味着矩阵 vc 中的值将在列 0 和 5 中。如果矩阵中的第五行矩阵 mc 是 2 3 ( p = 2, q = 3),这意味着 vc 中第五行的值将在第 2 列和第 3 列中。
Hope this time, i am more clear.
希望这一次,我更清楚了。
Thanks for your help
谢谢你的帮助
回答by Null Set
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.
单位矩阵总是方阵,但 mc 不是。当您打电话时,eye(mc, dim)我怀疑 eye 将 mc 视为一个暗矩阵,而实际上它是 2 个暗矩阵,并写入未分配的内存。
回答by e.James
You are not allocating enough memory for a square matrix in your call to malloc(). The correct size would be dimsquared, not just 2*dim.
您没有为调用中的方阵分配足够的内存malloc()。正确的大小将被dim平方,而不仅仅是2*dim。
This should do the trick:
这应该可以解决问题:
mc = (double *)malloc(dim * dim * sizeof(double));
vc = (double *)malloc(dim * dim * sizeof(double));
回答by fbstj
As far as I can tell the double free or corruption (!prev)is that you're calling free() multiple times on the same pointer, your other functions might be doing this (I suspect affiche().
Maybe try running it after export MALLOC_CHECK_=0on your shell?
据我所知double free or corruption (!prev),您在同一个指针上多次调用 free() ,您的其他函数可能正在执行此操作(我怀疑affiche()。也许尝试export MALLOC_CHECK_=0在您的 shell 上运行它?
回答by Jens Gustedt
You must have a stack corruption somewhere in your code. Compile with debug options and run your code through valgrind, it will tell you.
您的代码中一定有堆栈损坏。使用调试选项编译并通过 valgrind 运行您的代码,它会告诉您。
BTW, in C casting the result of mallocis a bad idea. Don't do that, it might hide the diagnostic of the lack of including the correct header file.
顺便说一句,在 C 中,结果malloc是一个坏主意。不要这样做,它可能会隐藏缺少包含正确头文件的诊断信息。

