gpt4 book ai didi

c - 尝试使用 valgrind 的输出调试 c 程序段错误

转载 作者:太空狗 更新时间:2023-10-29 15:14:21 28 4
gpt4 key购买 nike

我的一个 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-Tester 获取。

几个注意事项:我正在尝试帮助自己,我已经完成了研究,查看了类似的问题并自己发现了有关 valgrind 的信息。

虽然我仍然是一个相对初学者,虽然我很欣赏解决方案(如何修复它),但更有用的是知识(下次如何自己修复或避免它)。我在这里(并且非常热衷于)学习。

最佳答案

buff[i] = '\0';//终止字符串

这里 i == 11,因为你只分配了 11 个字符,while 条件在 i=11 时结束。
所以,你访问了一个你没有分配的内存。

未定义这种情况下的行为。

你可以通过在你的 malloc 上分配一个额外的字符来解决这个问题。

关于c - 尝试使用 valgrind 的输出调试 c 程序段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7123041/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com