| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 306 人关注过本帖
标题:请教一个关于realloc()函数的问题
只看楼主 加入收藏
shangxiaa
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-5-12
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
请教一个关于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的地址没有变化,但其中保存的已输入的字符串仅剩第一个,其他全部变成野指针。这是什么情况?
搜索更多相关主题的帖子: capacity include number 
2015-05-12 16:14
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:10 
弄一大堆英文  看不懂  

初步怀疑你的指针并没有被分配到空间

DO IT YOURSELF !
2015-05-13 08:43
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:10 
看不懂
2015-05-13 14:09
快速回复:请教一个关于realloc()函数的问题
数据加载中...
 
   



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

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