损坏的堆错误c

时间:2018-05-04 10:52:47

标签: c heap-corruption

我正在尝试从父母的矢量创建一棵树。但是,当我使用malloc创建节点时,我收到“损坏的堆错误”。它适用于前两个孩子,但在第三个孩子崩溃(或终止但不将根与孩子连接。)

  

lab7.exe中0x77E8A879(ntdll.dll)的未处理异常:0xC0000374:   堆已损坏(参数:0x77EC5910)。

我已经实现了这一点,首先提取根并创建节点,然后提取根的子节点并创建它们。

搜索root->搜索根并返回它的值。 搜索子项的功能(搜索键)

void create->创建子项的函数。我发送的矢量只包含该特定父项的子项,而不包含其他子项。

 *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

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

1 个答案:

答案 0 :(得分:0)

我认为问题出在您的main()方法中,malloc()使用sizeof(TreeNodeParent*)代替sizeof(TreeNodeParent),并将其分配给rootParent

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