C语言 Valgrind 多个“大小为 1 的无效写入/读取”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16574153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Valgrind multiple "invalid write/read of size 1" errors
提问by Austin Johnston
My program reads in people from a file and saves their information to a Person structure that looks like this:
我的程序从文件中读取人员并将他们的信息保存到如下所示的人员结构中:
struct person
{
char *fname;
char *lname;
int id;
};
typedef struct person Person;
These people are held in an array of Persons and the function to fill that array looks like this (the total variable is the total number of Persons counted from the file):
这些人保存在一个 Persons 数组中,填充该数组的函数如下所示(total 变量是从文件中计算的 Persons 总数):
Person* fillArray(int total, FILE *fin)
{
rewind(fin);
int i;
char temp[50];
char temp2[50];
Person *p = (Person*) calloc(total, sizeof(Person));
for(i = 0; i < total; i++)
{
fscanf(fin, "%s", temp);
p[i].fname = (char *) calloc(strlen(temp)+1, sizeof(char));
strcpy(p[i].fname, temp);
fscanf(fin, "%s", temp2);
p[i].lname = (char *) calloc(strlen(temp)+1, sizeof(char));
strcpy(p[i].lname, temp2);
fscanf(fin, "%d", &(p + i)->id);
}
return p;
}
I cleanup all my memory used and I calloc for the '\0' at the end of the temp strings. Not sure why I get these errors:
我清理了所有使用的内存,并为临时字符串末尾的 '\0' 进行了调用。不知道为什么我会收到这些错误:
HEAP SUMMARY:
==4736== in use at exit: 0 bytes in 0 blocks
==4736== total heap usage: 8 allocs, 8 frees, 414 bytes allocated
==4736==
==4736== All heap blocks were freed -- no leaks are possible
==4736==
==4736== ERROR SUMMARY: 10 errors from 2 contexts (suppressed: 0 from 0)
==4736==
==4736== 3 errors in context 1 of 2:
==4736== Invalid write of size 1
==4736== at 0x402C6E8: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==4736== by 0x804898E: fillArray (cscd240_s13_lab11Functions.c:30)
==4736== by 0x8048731: main (cscd240_s13_lab11Tester.c:13)
==4736== Address 0x41ef24f is 3 bytes after a block of size 4 alloc'd
==4736== at 0x402A5E6: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==4736== by 0x804896A: fillArray (cscd240_s13_lab11Functions.c:29)
==4736== by 0x8048731: main (cscd240_s13_lab11Tester.c:13)
==4736==
==4736==
==4736== 7 errors in context 2 of 2:
==4736== Invalid write of size 1
==4736== at 0x402C6C3: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==4736== by 0x804898E: fillArray (cscd240_s13_lab11Functions.c:30)
==4736== by 0x8048731: main (cscd240_s13_lab11Tester.c:13)
==4736== Address 0x41ef24c is 0 bytes after a block of size 4 alloc'd
==4736== at 0x402A5E6: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==4736== by 0x804896A: fillArray (cscd240_s13_lab11Functions.c:29)
==4736== by 0x8048731: main (cscd240_s13_lab11Tester.c:13)
==4736==
==4736== ERROR SUMMARY: 10 errors from 2 contexts (suppressed: 0 from 0)
Thank you for the help. I have looked through several threads and most of them are about not calloc/mallocing for the '\0' at the end of chars. I do this however I still get these errors and many more when I sort the Person array with qsort(). I apologize if this is the same problem that has been asked before, I could not find the thread.
感谢您的帮助。我查看了几个线程,其中大多数都不是关于字符末尾的 '\0' 的 calloc/mallocing。我这样做了,但是当我使用 qsort() 对 Person 数组进行排序时,我仍然会遇到这些错误以及更多错误。如果这与之前被问过的问题相同,我深表歉意,我找不到该线程。
回答by Daniel Fischer
You're using the wrong length for the allocation of the second part,
您在第二部分的分配中使用了错误的长度,
fscanf(fin, "%s", temp2);
p[i].lname = (char *) calloc(strlen(temp)+1, sizeof(char));
strcpy(p[i].lname, temp2);
That should use strlen(temp2);there.
那应该strlen(temp2);在那里使用。

