0
我有问题realloc。这是我的功能,它从输出中读取单词,并在检测到EOF时终止。 该函数使内存泄漏,以下程序将引发SIGSEGV或SIGABORT。什么是问题?动态指针数组realloc失败
int inx=0;
char **wordList=NULL;
int v;
char tmpArr[100];
do
{
v=scanf("%s",tmpArr);
if(v!=-1)
{
char* word=(char*)malloc(strlen(tmpArr)+1);
strcpy(word,tmpArr);
char**more=(char**)realloc(wordList,sizeof(char*)*(inx+1));
if(more!=NULL) {wordList=more;} else return 1;
wordList[inx++]=word;
printf("%d\n",inx);
}
}
你用调试器或添加了额外的'printf'语句找到该行的程序失败吗?代码示例末尾是否存在缺少“while”语句? – simonc
你确定没有要阅读的单词,大于99个字符吗? – alk
向malloc添加错误检查。不要投射malloc也不要realloc。 – alk