0

这是调试信息,

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() 不会更改第一个参数。谁能帮我?

PS这里是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));

我认为这部分是正确的。

4

2 回答 2

0

错误消息似乎很清楚,它是堆损坏。

尝试将 free() for 循环移到函数调用 k_means() 上方,并注释掉程序的其余部分。

我怀疑这是破坏堆的原因!

如果这个实验正确地释放了内存,你知道这个错误在 k_means() 函数中......

于 2013-06-22T04:06:32.860 回答
-1

您不使用从 calloc 中获得的指针地址,因此 free() 参数指向的内存起始地址不同。如果我没有错,您无法获得全部内存,然后将它们部分释放,如果这是您要实现的目标。至少使用malloc。

在 free() 中使用与 calloc() 相同的值。

于 2013-06-21T16:46:51.700 回答