繁体   English   中英

C编程Trie树堆缓冲区溢出

[英]C programming Trie tree heap buffer overflow

我刚刚开始编程,并有一个问题:我想在特里树中插入大量单词。 然后遍历树并释放所有节点,以便我可以再次插入这些单词。 但是,当单词数很大(比如说一百万)时,我遇到了堆缓冲区溢出的情况,这些函数适用于数量较少的单词:

这是节点

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


要插入的功能:

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;
};


以及免费功能:

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

谢谢!

检查malloc函数的返回。 如果为NULL,则表示您已达到此过程的堆内存最大值,因此malloc无法为您分配额外的内存。

暂无
暂无

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

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