gpt4 book ai didi

c - free() 期间堆损坏

转载 作者:行者123 更新时间:2023-11-30 18:14:15 26 4
gpt4 key购买 nike

这是调试信息,

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com