| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 553 人关注过本帖
标题:关于一个字典树内存释放问题(有内存泄漏),不知道如何修改,望大神指教
只看楼主 加入收藏
cjbs10086
Rank: 2
等 级:论坛游民
帖 子:32
专家分:45
注 册:2012-10-13
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:3 
关于一个字典树内存释放问题(有内存泄漏),不知道如何修改,望大神指教
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define X_LONGSEN  1024
#define Y_LONGWORD 1024
char z_Str[Y_LONGWORD];
#define _CRT_SECURE_NO_WARNINGS

struct node
{
    char MWord[Y_LONGWORD]; //对应的最高权权值
    int order;              //权值、并作为标记是否有词
    struct node *next[16];
};
struct node * Creat_Tree(struct node * T);
void insertTree(char *str, struct node *T, char *MaxWord, int num_max);
void findStr(char *str,struct node *T);
int findNum(char *str, struct node *T);
int CodeConvert(char *szFromStr, int iFromLen, char *szToStr, int iToLen);
struct node * Creat_Tree(struct node * T)
{
    FILE *fq;
    int i,num_max,lenGetSen, leWord, leSen;
    char GetSentence[X_LONGSEN] = "",GetWord[Y_LONGWORD] = "" ,ToWord[Y_LONGWORD] = "",strhan[Y_LONGWORD] = "",MaxWord[Y_LONGWORD] = "",hanMax[Y_LONGWORD] = "" ;
    T = (struct node *)malloc(sizeof(struct node));
    memset (T->MWord, 0 ,sizeof(T->MWord));
    T->order = -1;
    for(i = 0; i < 16; i++)
        T->next[i] = NULL;
    memset(GetSentence,0,sizeof(GetSentence));
    fq = fopen("DataInfo\\test.txt","r+");
    while (fgets (GetSentence,500, fq) != NULL) //读取权值文档,建立各个词对应的最高权值
    {
        lenGetSen = strlen(GetSentence);
        leSen = 0;
        memset (MaxWord, 0, sizeof(MaxWord));
        leWord = 0;
        leWord = 0;
        while(GetSentence[leSen] != '&' && (GetSentence[leSen] < '0' || GetSentence[leSen] >'9'))
            MaxWord[leWord++] = GetSentence[leSen++];
        //取最高权词的权值
        num_max = 0;
        while(GetSentence[leSen] >= '0' && GetSentence[leSen] <= '9')
            num_max = num_max*10 + GetSentence[leSen++] - '0';
        leSen++;
        while (GetSentence[leSen] != ']' && leSen < lenGetSen)
        {
            memset (GetWord, 0, sizeof(GetWord));
            memset (ToWord, 0, sizeof(ToWord));
            leWord = 0;
            while (GetSentence[leSen] != '-')
            {
                GetWord[leWord++] = GetSentence[leSen++];
            }
            CodeConvert (GetWord, strlen(GetWord), ToWord, sizeof(ToWord));
            insertTree (ToWord, T, MaxWord, num_max);
            while (GetSentence[leSen] == ' ' || ( GetSentence[leSen] >='0' && GetSentence[leSen] <= '9') || GetSentence[leSen] == '-')
                leSen++;
        }
    }
    return T;
}

//字典树的内存释放
void free_T(struct node *T)
{
    int i;
    for(i = 0; i < 16 ; i++)
    {
        if(T->next[i] != NULL)
        {
            free_T(T->next[i]);
            T->next[i]=NULL;
        }
    }
    free(T);
}
void insertTree(char *str,struct node *T, char *MaxWord, int num_max)
{
    int len, i, j, flag=0, id = 0;
    struct node *p, *q;
    p = T;
    len = strlen(str);
    for (i = 0; i < len; i++)
    {
        if(str[i]>= 'a' && str[i] <= 'f')//当时abcdef时 转化为数字
            id = str[i]- 'a' + 10;
        else
            id = str[i] - '0';
        if( p ->next[id] == NULL)//扩展节点
        {
            flag = 1;
            q = (struct node *)malloc(sizeof(struct node) + 1);
            memset(q->MWord,0,sizeof(q->MWord));
            q->order = -1;
            for(j = 0;j < 16 ;j++)
                q ->next[j] = NULL;
            p->next[id] = q;

        }
        p = p->next[id];
    }
    if(flag)
    {
        strcpy(p->MWord, MaxWord);
        p->order = num_max;
    }
    else
    {
        if( p -> order == -1)
        {
            strcpy(p->MWord, MaxWord);
            p->order = num_max ;
        }
    }
}
int CodeConvert(char *szFromStr, int iFromLen, char *szToStr, int iToLen)
{
    int i, iNum;
    char szByte[4];
    char *szBuf;

    //检查储存空间是否足够
    if (iToLen<=iFromLen || !szFromStr || !szToStr)
        return 0;

    //判断原文是否已经为制定URL ANSI码
    if(iFromLen%3 == 0)
    {
        iNum = iFromLen/3;
        for (i=0; i<iNum; i++)
        {
            if(szFromStr[i*3] != '%')
                break;
        }
        if(iNum == i)
        {
            strcpy(szToStr, szFromStr);
            return 1;
        }
    }

    if(iToLen<(2*iFromLen+1))
        return 0;
    szBuf = (char *)malloc(iToLen);
    memset(szBuf, 0, iToLen);

    //按字节转换
    for (i=0; i<iFromLen; i++)
    {
        strcat(szBuf, "%");
        sprintf(szByte, "%x", (unsigned char)szFromStr[i]);
        strcat(szBuf, szByte);
    }

    strcpy(szToStr, szBuf);
    free(szBuf);
    szBuf = NULL;
    return 1;
}
int main()
{
    struct node *T = NULL;
    T = Creat_Tree(T);
    free_T(T);
    return 0;
}
搜索更多相关主题的帖子: include 如何 
2013-08-28 11:50
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:20 
估计没有谁愿意来帮你找bug的

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-09-03 20:11
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:0 
如果确认内存泄漏,而且就怎么一点代码,找出来不难吧。
调试下咯

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-09-03 20:11
cjbs10086
Rank: 2
等 级:论坛游民
帖 子:32
专家分:45
注 册:2012-10-13
收藏
得分:0 
关键是我不知道为什么字典树内存没有完全释放
2013-09-03 20:35
快速回复:关于一个字典树内存释放问题(有内存泄漏),不知道如何修改,望大神指 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021411 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved