这是调试信息,
HEAP[opencv_CoTraining2.exe]: Heap block at 0AD15168 modified at 0AD15594 past requested size of 424
Windows has triggered a breakpoint in opencv_CoTraining2.exe.
This may be due to a corruption of the heap, which indicates a bug in opencv_CoTraining2.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while opencv_CoTraining2.exe has focus.
The output window may have more diagnostic information.
这是我的代码:
void GetKCent(Mat& mat)
{
double** tmp=(double**)calloc(mat.rows,sizeof(double*));
double f[128];
memset(f,0,sizeof(f));
double max=0;
for (int i=0;i<mat.rows;i++)
{
tmp[i]=(double*)calloc(mat.cols,sizeof(double));
for (int j=0;j<mat.cols;j++)
{
tmp[i][j]=mat.at<float>(i,j);
if (tmp[i][j]>max) max=tmp[i][j];
}
}
for (int i=0;i<mat.cols;i++) for (int j=0;j<mat.rows;j++) tmp[j][i]/=max;
k_means(tmp,mat.rows,128,K_CLUSTER,KMEANSDIS,kcent);
for (int i=0;i<K_CLUSTER;i++) for (int j=0;j<128;j++) kcent[i][j]*=max;
for (int i=0;i<mat.rows;i++)free(tmp[i]);
free(tmp);
}
这条线发生了故障,
for (int i=0;i<mat.rows;i++)free(tmp[i]);
并且函数k_means()不会更改第一个参数。谁能帮我?
附言
这是k_means()的定义
int k_means(double **data, int n, int m, int k, double t, double **centroids)
这是_double ** kcent_
kcent=(double**)calloc(K_CLUSTER,sizeof(double*));
for (int i=0;i<K_CLUSTER;i++) kcent[i]=(double*)calloc(128,sizeof(double));
我认为这部分是正确的。
最佳答案
该错误消息似乎非常清楚,它是堆损坏。
尝试将free()for循环移至函数调用k_means()上方,并注释掉程序的其余部分。
我怀疑这就是破坏堆的原因!
如果此实验正确释放了内存,则说明该错误存在于k_means()函数中。
关于c - free()期间堆损坏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17240089/