malloc.c:2451:sYSMALLOc断言失败

时间:2014-02-21 16:10:02

标签: c

我一直收到这个错误:

  

malloc.c:2451:sYSMALLOc:断言(old_top ==(((mbinptr)(((char *)&amp;((av) - &gt; bins [((1) - 1)* 2])) - __builtin_offsetof(struct malloc_chunk,fd))))&amp;&amp; old_size == 0)|| ((unsigned long)(old_size)&gt; =(unsigned long)(((__ builtin_offsetof(struct malloc_chunk,fd_nextsize))+((2 *(sizeof(size_t))) - 1))&amp;〜((2 * (sizeof(size_t))) - 1)))&amp;&amp;((old_top) - &gt; size&amp; 0x1)&amp;&amp;((unsigned long)old_end&amp; pagemask)== 0)'失败。< / p>

这是导致错误的代码段:

BinaryTree * oldTree, * temp;
BinaryTree ** sorted;
int i = 0, j = 0, k = 0;

oldTree = (BinaryTree*)malloc(cnt * sizeof(BinaryTree));
sorted = (BinaryTree**)malloc(cnt * sizeof(BinaryTree*));

/*
for (i = 0; i < cnt; i++)
{
    oldTree[i].value = table[i].data;
    oldTree[i].freq = table[i].cnt;
    oldTree[i].left = NULL;
    oldTree[i].right = NULL;
    oldTree[i].parent = NULL;
    sorted[i] = &oldTree[i];
}

while (cnt >= 2)
{
    temp = (BinaryTree*)malloc(sizeof(BinaryTree));

    temp->value = -1;
    temp->freq = sorted[0]->freq + sorted[1]->freq;
    temp->left = sorted[0];
    temp->right = sorted[1];
    temp->parent = NULL;
    sorted[0]->parent = temp;
    sorted[1]->parent = temp;

    for (j = 0; j < cnt - 2; j++)
        sorted[j] = sorted[j + 2];

    j = 0;
    while (temp->freq >= sorted[j]->freq && j < cnt - 2)
        j++;

    for (i = cnt - 1; i > j; i--)
        sorted[i] = sorted[i - 1];

    sorted[j] = temp;

    cnt--;
    k++;
}
createPattern(oldTree, table, cnt); 

free(oldTree); // May be wrong
free(temp);
free(sorted);
*/

甚至注释掉这段代码的每一部分,除了malloc行本身导致了这个错误。

有什么想法吗?

0 个答案:

没有答案