损坏的堆错误 c

Corrupted Heap error c

我正在尝试从 parent 的向量创建一棵树。但是,当我使用 malloc 创建节点时,我得到了 "corrupted heap error"。它适用于前两个 children ,但在第三个时崩溃(或终止但不将根与 child 连接。)

Unhandled exception at 0x77E8A879 (ntdll.dll) in lab7.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77EC5910).

我是这样实现的,首先提取根并创建节点,然后提取根的 children 并创建它们。

搜索根->搜索根和returns它的值。 搜索 children(search key )

的函数

void create->创建children的函数。我发送的向量只包含那个特定 parent 的 children 而不是其他 children.

 *typedef struct  node
    {
        int value;
        node *left;
        node *right;
        node *middle;
    }TreeNodeParent;
    int search_root(int in[9])
    {
        for (int i = 0; i < 9; i++)
        {
            if (in[i] == -1)
            {
                int var = i+1;
                in[i] = -2;
                return var;
            }// pe else nu facem nimic
        }
        return -2; 
    }
    int search_key(int in[9], int  radacina)
    {
        for (int i = 0; i < 9; i++)
        {
            if (in[i] == radacina)
            {

                int var = i + 1;
                in[i] = -2;
                return var;
            }
        }
        return -3;
    }
    TreeNodeParent *createOneNode(int value)
    {
//the error appears here
        TreeNodeParent* create = (TreeNodeParent*)malloc(sizeof(TreeNodeParent));
        create->value = value;
        create->left = create->middle = create->right = NULL;
        return create;
    }

    void create(int vector[], TreeNodeParent* radacina)
    {
        for (int i = 0; i < 9; i++)
        {
            if (vector[i] == -3)//am stabilit ca -3 ii oprirea
            {
                break;
            }
            else
            {
                TreeNodeParent* create = createOneNode(vector[i]);

                if (radacina->left == NULL)
                {
                    radacina->left = create;
                }
                else if (radacina->middle == NULL)
                {
                    radacina->middle = create;
                }
                else 
                {
                    radacina->right = create;
                }
            }
        }
    }
    int main()
    {
        int input[9] = { 2,7,5,2,7,7,-1,5,2 };
        int root = search_root(input);
        if (root == -2)
        {
            printf("Nu gasim radacina, arbore incorect!");
        }
        else { printf("root %d", root); }

        //crearea nodului parinte
        TreeNodeParent* rootParent = (TreeNodeParent*)malloc(sizeof(TreeNodeParent*));
        rootParent->value = root;
        rootParent->left = NULL;
        rootParent->right = NULL;
        rootParent->middle = NULL;
        int vect2[9];
        for (int i = 0; i < 9; i++)//worst case, tot arborele is copii ai lui root->o(n2)
        {
             vect2[i] = search_key(input, root);
             printf("copii rootului %d", vect2[i]);
            if ( vect2[i] == -3)
            {
                break;
            }
        }   
        create(vect2,rootParent);
        _getch();
        return 0;
    }

用 gdb 在线检查:

>     tin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((ol
>     d_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
program exited
> with code 134

我不明白为什么函数只在第三次(而不是之前)崩溃。它并不总是出现。有时它工作正常,有时它会因该错误而停止。 另外,如果有更好的方法来创建具有 parent 表示的树?

我认为问题出在你的 main() 方法中,你 malloc() 使用 sizeof(TreeNodeParent*) 而不是 sizeof(TreeNodeParent) 并将其分配给 rootParent:

//crearea nodului parinte
//TreeNodeParent* rootParent = (TreeNodeParent*)malloc(sizeof(TreeNodeParent*));
TreeNodeParent* rootParent = (TreeNodeParent*)malloc(sizeof(TreeNodeParent));      `