各位lz帮我看下为什么free()函数放在do while()循环用不了
#include<stdio.h>#include<string.h>
#include<stdlib.h>
typedef struct word word;
struct word
{
char* pword;
unsigned int count;
word* pnext;
};
int main()
{
word* pfirst=NULL;
word* pcurrent=NULL;
word* previous=NULL;
#define BUF_LEN 101
#define START 100
#define WORD 20
word* ptemp=NULL;
char buf[BUF_LEN];
char delimites[]=" \n\".,;!?)(";
size_t str_size=START;
size_t word_max=20;
unsigned int word_count=0;
unsigned int i=0;
size_t word_length=0;
char ch='y';
int a=0;
int flag=0;
char* pstr=NULL;
char* pbuf=NULL;
while(ch=='y')
{
pbuf=(char*)calloc(str_size,sizeof(char));
if(!pbuf)
{
printf("内存分配出现错误");
return 1;
}
*pbuf='\0';
fflush(stdin);
printf("请每行输入最多%d字符,\n""结束输入通过按一个空行",BUF_LEN-1);
while(a==0)
{
if(! fgets(buf,sizeof(buf),stdin))
{
printf("请错误:");
return 9;
}
if(buf[0]=='\n')
break;
if(! strcat(pbuf,buf))
printf("连接错误:");
}
pstr=strtok(pbuf,delimites);
printf("正确");
if(!pstr)
{
printf("没找到");
return 2;
}
while(pstr)
{
pcurrent=pfirst;
while(pcurrent)
{
if(strcmp(pcurrent->pword,pstr)==0)
{
flag=1;
++pcurrent->count;
break;
}
pcurrent=pcurrent->pnext;
flag=0;
}
if(flag==0)
{
pcurrent=(word*)calloc(1,sizeof(word));
if(! pcurrent)
{
printf("分配内存错误");
return 1;
}
word_length=10;
if(!pfirst)
pfirst=previous=pcurrent;
else
{
previous->pnext=pcurrent;
previous=pcurrent;
}
pcurrent->pword=calloc(word_length,sizeof(char));
strcpy(pcurrent->pword,pstr);
pcurrent->count=1;
pcurrent->pnext=NULL;
++word_count;
}
pstr=strtok(NULL,delimites);
}
pcurrent=pfirst;
i=0;
while(pcurrent)
{
printf("%s %d",pcurrent->pword,pcurrent->count);
pcurrent=pcurrent->pnext;
i++;
if(i%4==0)
printf("\n");
}
free(pbuf);
pbuf=NULL;
pcurrent=pfirst;
while(pcurrent)
{
free(pcurrent->pword);
pcurrent->pword=NULL;
ptemp=pcurrent;
pcurrent=pcurrent->pnext;
free(ptemp);
ptemp=NULL;
}
printf("还想在来一次吗(y继续n退出)");
scanf("%c",&ch);
}
return 0;
}