请教一个关于realloc()函数的问题
#define __STDC_WANT_LIB_EXT1__ 1#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<stdlib.h>
#define BUF_LEN 100 //Length of input buffer
#define COUNT 5 //Initial number of srtings
int main(void)
{
char buf[BUF_LEN]; //Input buffer
size_t str_count=0; //Current string count
size_t capacity=COUNT; //Current maximun number of strings
char** pS=(char**)calloc(capacity,sizeof(char*)); //Pointers to strings
char** psTemp=NULL; //Temporary pointer to pointer to char
char* pTemp=NULL; //Temporary pointer to char
size_t str_len=0; //Length of a string
bool sorted=false; //Indicated when strings are sorted
printf("Enter strings to be sorted,one per line.Press Enter to end:\n");
//Read in all the strings
char* ptr=NULL;
while(true)
{
ptr=fgets(buf,BUF_LEN,stdin);
if(!ptr) //Check for read error
{
printf("Error reading string.\n");
free(pS);
pS=NULL;
return 1;
}
if(*ptr=='\n') //Empty line check
break;
if(str_count==capacity)
{
capacity+=capacity/4; //Increase capacity by 25%
if(!(psTemp=(char**)realloc(pS,capacity))) //出错的行
return 1;
pS=psTemp;
}
//str_len=strnlen_s(buf,BUF_LEN)+1;
str_len=strnlen(buf,BUF_LEN)+1;
if(!(pS[str_count]=(char*)malloc(str_len)))
return 2;
//strcpy_s(pS[str_count++],str_len,buf);
strcpy(pS[str_count++],buf);
}
//Sort the string in ascending order
while(!sorted)
{
sorted=true;
for(size_t i=0;i<str_count-1;++i)
{
if(strcmp(pS[i],pS[i+1])>0)
{
sorted=false; //We werw out of order so...
pTemp=pS[i]; //swap pointer pS[i]...
pS[i]=pS[i+1]; //and...
pS[i+1]=pTemp; //pS[i+1]
}
}
}
//Output the sorted strings
printf("Your input sorted in ascending sequence is:\n\n");
for(size_t i=0;i<str_count;++i)
{
printf("%s",pS[i]);
free(pS[i]); //Release memory for the word
pS[i]=NULL; //Reset the pointer
}
free(pS); //Release memory for pointers
pS=NULL; //Reset the pointer
return 0;
}
上述函数再用Dev-C++运行时总是出错。调试发现,输入6行字符以后,也就是执行realloc()函数以后,虽然指针pS的地址没有变化,但其中保存的已输入的字符串仅剩第一个,其他全部变成野指针。这是什么情况?