关于一个字典树内存释放问题(有内存泄漏),不知道如何修改,望大神指教
#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;
}