【发布时间】:2011-08-19 14:26:36
【问题描述】:
我的一个 CLI 程序在 Windows 上编译并运行良好。在 linux 上编译正常,但在运行时会导致分段错误。
我向 stackoverflow 寻求帮助,发现一些问题类似于我将要提出的建议 valgrind 的问题,而我恰好安装了(哇!)。
所以我通过 valgrind 运行我的程序,得到了令人沮丧的大量输出,但我将从第一条错误消息开始:
==11951== Command: ./vt
==11951==
Loading...
Load default database? (y/n)y
Opened input file vtdb.~sv, reading contents...
==11951== Invalid write of size 1
==11951== at 0x400FA9: readnumberfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x400C21: getrecordsfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x401FFD: main (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== Address 0x53b05bb is 0 bytes after a block of size 11 alloc'd
==11951== at 0x4C28FAC: malloc (vg_replace_malloc.c:236)
==11951== by 0x400EAC: readnumberfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x400C21: getrecordsfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x401FFD: main (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951==
...finished.
1180 entries read from vtdb.~sv.
问题好像出在readnumberfromfile,我翻遍了,好像没发现什么问题!
谁能解释一下?
int readnumberfromfile (int maxvalue,char separator)
{
int number, i=0;
char ch;
char * buff = (char *)malloc(11);//allocate enough space for an 10-digit number and a terminating null
if (!buff) {printf("Memory allocation failed!\n");return 0;}//return 0 and print error if alloc failed
if (!maxvalue) maxvalue=MAXINTVALUE;
ch=getc(inputfile);
while (!isdigit(ch))
{
if (ch == separator||ch=='\n'||ch==EOF) {fprintf(stderr,"Format error in file\n");return 0;}//if no number found(reached separator before digit), print error and return 0
ch = getc(inputfile);//cycle forward until you reach a digit
}
while (i<11 && ch!=separator && ch!='\n')//stop when you reach '~', end of line, or when number too long
{
buff[i++]=ch;
ch = getc(inputfile); //copy number from file to buff, one char at a time
}
buff[i] = '\0';//terminate string
number = atoi(buff)<=maxvalue ? atoi(buff) : maxvalue;//convert string to number and make sure it's in range
free(buff);
return number;
}
如果有任何用处,则从 getrecordsfromfile 调用:
void getrecordsfromfile(char * inputfilename,char separator)
{
int counter = 0;
struct vocab * newvocab;
struct listinfo * newvocablist;
if (!(inputfile = fopen(inputfilename, "r")))
{
printf("Unable to read input file. File does not exist or is in use.\n");
}
else
{
printf("Opened input file %s, reading contents...\n",inputfilename);
while (!feof(inputfile))
{
newvocab = (struct vocab *)malloc(sizeof(struct vocab));
if (!newvocab)
{
printf("Memory allocation failed!\n");
return;
}
else
{
newvocab->question=readtextfromfile(MAXTEXTLENGTH,separator);
newvocab->answer=readtextfromfile(MAXTEXTLENGTH,separator);
newvocab->info=readtextfromfile(MAXTEXTLENGTH,separator);
newvocab->hint=readtextfromfile(MAXTEXTLENGTH,separator);
newvocab->right=readnumberfromfile(1,separator);
newvocab->counter=readnumberfromfile(0,separator);
newvocab->known=readnumberfromfile(3,separator);
switch (newvocab->known)
{
case 0: newvocablist = &n2l;break;
case 1: newvocablist = &norm;break;
case 2: newvocablist = &known;break;
case 3: newvocablist = &old;break;
}
addtolist(newvocab,newvocablist);
if (newvocab->question==NULL||newvocab->answer==NULL)
{
printf("Removing empty vocab record created from faulty input file...\n");
removefromlist(newvocab,newvocablist,1);
}
else counter++;
}
}
fclose(inputfile);
printf("...finished.\n%i entries read from %s.\n\n",counter,inputfilename);
}
return;
}
完整源码可以从https://github.com/megamasha/Vocab-Testergitt
一些注意事项:我正在努力帮助自己,我已经完成了我的研究,查看了类似的问题,并且我自己发现了有关 valgrind 的信息。
虽然我仍然是一个相对初学者,虽然我很欣赏解决方案(如何解决它),但更有用的是知识(下次如何自己解决或避免它)。我在这里(并且非常渴望)学习。
【问题讨论】:
-
我不知道这是否与您的问题有关,但是
getc不会返回char,它会返回int。 -
另外,您可能会因格式错误的文件而发生内存泄漏。需要在 fprintf(stderr,"Format error in file\n"); 之后 free(buff)
-
@Oli:多么具有误导性的函数名称!我不知道。一个 char 基本上是一个字节,对吧?一个 int 有多大?我以前没有遇到过使用 getc 阅读文本的问题(这似乎是我一直在做的事情)我应该担心吗?有人吗?
-
@Seth:谢谢——好点子。功能适当修改。我应该看看我的 readtextfromfile 函数是否有类似的遗漏。
-
请参阅stackoverflow.com/questions/1782080/… 了解有关 EOF 的一些讨论。
标签: c debugging segmentation-fault valgrind