简体   繁体   English

C编程Trie树堆缓冲区溢出

[英]C programming Trie tree heap buffer overflow

I just started programming and have a question: I want to insert a large amount of words into a trie tree. 我刚刚开始编程,并有一个问题:我想在特里树中插入大量单词。 Then traverse the tree and free all the nodes so that I can insert these words again. 然后遍历树并释放所有节点,以便我可以再次插入这些单词。 But when the number of words is large (say 1 million), I hit a heap buffer overflow, these function works for smaller amount of words: 但是,当单词数很大(比如说一百万)时,我遇到了堆缓冲区溢出的情况,这些函数适用于数量较少的单词:

Here is the nodes 这是节点

struct node
{
    struct node * parent;
    int noempty;
    int isword;
    int super;
    int occurrence;
    int leaf;
    struct node * child[26];
};


The function to insert: 要插入的功能:

struct node* insert(struct node *root,char *c)
{
    int i=0;
    struct node *temp=root;
    int l=length(c);
    while(i!=l)
    {
        int index=c[i]-'a';
        if(temp->child[index]==NULL)
        {
            //New Node
            struct node *n=malloc(sizeof(struct node)); 
            n->parent=temp;
            temp->child[index]=n;
            temp->noempty=1;
        }
        //Node Exist
        if(i!=l&&temp->leaf==1)
        { 
            temp->leaf=0;
        }
        temp=temp->child[index];
        i++;
    }
    if(temp->noempty==0)
    {
        temp->leaf=1;
    }
    temp->isword=1;
    return root;
};


And the free function: 以及免费功能:

void freetree(struct node* curs)
{ 
    int i;
    if(!curs) 
        return;  
    for (i = 0; i !=26; i++)
        freetree(curs->child[i]);
    free(curs);
}

Thank you! 谢谢!

Check for the return of the malloc function. 检查malloc函数的返回。 If it is NULL, it means you have reached the maximum of your heap memory for this process, and thus malloc cannot allocate extra memory for you. 如果为NULL,则表示您已达到此过程的堆内存最大值,因此malloc无法为您分配额外的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM