劳驾谁帮我看看这段代码哪里泄漏了,泄漏的地方很多嘿嘿#include<stdio.h>
#include<stdlib.h>
#include<string.h>#define PATH "d:\\text.txt";void GET_STRING();
int cmp( const void *a ,const void *b);
void main()
{
GET_STRING();//文件中讀出的數據
}struct sort1
{
char *sort_word;
int sort_count;
};//定義最終排序的結構體
void GET_STRING()
{
FILE *fp=NULL;
int length=0;
char *pt=PATH;
fp=fopen(pt,"r+");
fseek(fp,0L,SEEK_END);
length=ftell(fp);
fseek(fp,0L,SEEK_SET);
char *p=(char *)malloc(length+2*sizeof(char));
for(int i=0;i<length+1;i++)
*(p+i)=NULL;
char *temp=(char *)malloc(length+1*sizeof(char));

char *cp=NULL;
while(!feof(fp))
{
cp =fgets(temp,length+1,fp);
if(!cp) 
break;
strcat(p,temp);
}
*(p+strlen(p)+1)='\0';
*(p+strlen(p))=' ';

free(temp);
temp=NULL;
fclose(fp); for(i=0;i<int(strlen(p)+1);i++)
{
if(*(p+i)=='\n'||*(p+i)=='\t'||*(p+i)=='\r')
{
*(p+i)=' ';
}
}
//printf("%d\n",strlen(p));
//printf("%s\n",p);

//-----------------------------------------------------------------------
char **word=new char*[length];
for(i=0;i<length;i++)
{
word[i]=(char *)malloc((length+1)*sizeof(char));
}
int temp_1=0;
int index=0;
int wi=0;
for(index=0;index<int(strlen(p));index++)
{
if(*(p+index)==' ')
{
int add=0;
bool b=false;
for(int j=temp_1;j<index;j++)
{
*(word[wi]+add)=tolower(*(p+j));//tolow to insert the arr
add++;
b=true;
}
if(b)
{
*(word[wi]+add)='\0';
wi++;//一共有多少個單詞
}
temp_1=index+1;
}
}
free(p);
p=NULL;
//for(i=0;i<wi;i++)
// printf("%s\n",word[i]);
//-----------------------------------------------------------------------
struct sort1 *str_sort=new sort1[length];
//for(i=0;i<length/2;i++)
//str_sort[i].sort_word =(char *)malloc(length*sizeof(char));
//-----------------------------------------------------------------------
int mk=0;
int aaa=0;
int si=0;
for(int m=0;m<wi;m++)
{
if(*word[m]!='\t')
{
mk=1;//單詞出現個數計數器
for(int n=m+1;n<wi;n++)
{
if(strcmp(word[m],word[n])==0)
{
mk++;
*word[n]='\t';//標記已經統計過的單詞
}
}
aaa=aaa+mk;//check the word loss
str_sort[si].sort_word=word[m];
str_sort[si].sort_count=mk;
si++;//出現不同單詞的個術統計
}
}
qsort(str_sort,si,sizeof(str_sort[0]),cmp);
for(i=0;i<si;i++)
printf("%-20s%5d\n",str_sort[i].sort_word ,str_sort[i].sort_count );
//printf("%d\n",wi);//check the word loss
//printf("%d\n",aaa);//check the word loss
printf("%s\n",wi==aaa ? "CHECK PASS\a":"ERROR\a\a\a");//check the word loss
for(i=0;i<length;i++)
{
free(word[i]);
word[i]=NULL;
//printf("A:%d\n",i);
//free(str_sort[i].sort_word);
//str_sort[i].sort_word=NULL;
//printf("B:%d\n",i);
}
delete(*word);
delete(str_sort);
}int cmp( const void *a ,const void *b)
{
if(((sort1 *)a)->sort_count > ((sort1 *)b)->sort_count)
return 1;
else
return -1;
}

解决方案 »

  1.   

    应该没有内存泄漏,不过代码问题还是不少。提几点建议1.注意变量命名和书写风格2.尽量不要把 new/delete 和 malloc/free 混用3.效率问题。word占用的空间是n平方级别。如果文件很多(例如一兆),内存是不够用的。其他一些查找之类的地方效率不高。
    其实保存单词的结构体数组完全可以换成链表,一边插入一边申请,需要多少申请多少。4.fopen new等很可能失败的的操作之后没有判断。5.某些地方逻辑比较混乱,就是说不明白为啥那样写。例如
    char *p=(char *)malloc(length+2*sizeof(char)); //为什么申请二倍空间?
    for( i=0;i <length+1;i++) *(p+i)=NULL;        //很少有把NULL赋给非指针的变量。初始化空间可以用memset函数
      

  2.   

    诶呀,谢谢你的回复哈,我是做测试的,没事儿在学学C的东西,我知道我编码超不规范,到现在还极度推崇用goto嘿嘿,得慢慢从新规范嘻嘻
      

  3.   

    是的,保存单词结构体数组是应该用链表的,当时做这个题的时候没看到链表那一章嘿嘿,就硬来了嘿嘿
    关于初始化空间也是的,我后来看书才看到memset这一系列的函数嘻嘻