valgrind错误条件跳转
Valgrind error conditional jump
我正在编写一个加载输入直到输入特定单词的程序,在本例中是单词 "konec"。虽然我的程序似乎工作正常,但我无法解决这个 Valgrind错误
==16573== Memcheck, a memory error detector
==16573== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==16573== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==16573== Command: ./s_main_o
==16573==
==16573== Conditional jump or move depends on uninitialised value(s)
==16573== at 0x4C2A020: strcmp (mc_replace_strmem.c:711)
==16573== by 0x4008D7: main (main.c:41)
==16573== Uninitialised value was created by a heap allocation
==16573== at 0x4C28CCE: realloc (vg_replace_malloc.c:632)
==16573== by 0x40089C: main (main.c:38)
==16573==
==16573== Conditional jump or move depends on uninitialised value(s)
==16573== at 0x4C2A024: strcmp (mc_replace_strmem.c:711)
==16573== by 0x4008D7: main (main.c:41)
==16573== Uninitialised value was created by a heap allocation
==16573== at 0x4C28CCE: realloc (vg_replace_malloc.c:632)
==16573== by 0x40089C: main (main.c:38)
==16573==
==16573==
==16573== HEAP SUMMARY:
==16573== in use at exit: 0 bytes in 0 blocks
==16573== total heap usage: 8 allocs, 8 frees, 1,125 bytes allocated
==16573==
==16573== All heap blocks were freed -- no leaks are possible
==16573==
==16573== For counts of detected and suppressed errors, rerun with: -v
==16573== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)
这里使用的部分代码
int main() {
int numberOfWords, i;
char** words;
char* word;
int* rarity;
char* konec = "konec";
int amount = 0;
double percentage;
words = malloc(10 * sizeof (char*));
rarity = calloc(256, sizeof (int));
numberOfWords = 0;
words[0] = 0;
int working = 1;
while (working == 1) {
int length = 0;
word = calloc((length + 1),sizeof (char));
char c;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n') {
break;
}
length++;
word = realloc(word, length + 1);
word[length - 1] = c;
}
if (strcmp(word, konec) == 0) {
working = 0;
free(word);
break;
}
}
}
我发现很多主题都在讨论同一问题,但我找不到解决方案 anyway.Thanks 来回答您的问题。
问题是您没有在重新分配时添加空终止符:
word = realloc(word, length + 1);
word[length - 1] = c;
此时,word
字符串未终止,因此 strcmp
可能会在搜索空终止符时结束。例如,当您键入 "ko"
时,strcmp
将确定字符 0 和 1 相同,并尝试检查 word[2]
- 您的程序未设置的位置。
添加此行以解决问题:
word[length] = '[=11=]';
当与 konec
的比较不成功时,您还应该将代码添加到 free
word
。
注意:您没有正确使用 realloc
:与其将其分配回 word
,不如将其分配给 temp
,并检查它是否为 NULL
。否则,当 realloc
失败时,您将无法释放先前分配的字。
我正在编写一个加载输入直到输入特定单词的程序,在本例中是单词 "konec"。虽然我的程序似乎工作正常,但我无法解决这个 Valgrind错误
==16573== Memcheck, a memory error detector
==16573== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==16573== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==16573== Command: ./s_main_o
==16573==
==16573== Conditional jump or move depends on uninitialised value(s)
==16573== at 0x4C2A020: strcmp (mc_replace_strmem.c:711)
==16573== by 0x4008D7: main (main.c:41)
==16573== Uninitialised value was created by a heap allocation
==16573== at 0x4C28CCE: realloc (vg_replace_malloc.c:632)
==16573== by 0x40089C: main (main.c:38)
==16573==
==16573== Conditional jump or move depends on uninitialised value(s)
==16573== at 0x4C2A024: strcmp (mc_replace_strmem.c:711)
==16573== by 0x4008D7: main (main.c:41)
==16573== Uninitialised value was created by a heap allocation
==16573== at 0x4C28CCE: realloc (vg_replace_malloc.c:632)
==16573== by 0x40089C: main (main.c:38)
==16573==
==16573==
==16573== HEAP SUMMARY:
==16573== in use at exit: 0 bytes in 0 blocks
==16573== total heap usage: 8 allocs, 8 frees, 1,125 bytes allocated
==16573==
==16573== All heap blocks were freed -- no leaks are possible
==16573==
==16573== For counts of detected and suppressed errors, rerun with: -v
==16573== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)
这里使用的部分代码
int main() {
int numberOfWords, i;
char** words;
char* word;
int* rarity;
char* konec = "konec";
int amount = 0;
double percentage;
words = malloc(10 * sizeof (char*));
rarity = calloc(256, sizeof (int));
numberOfWords = 0;
words[0] = 0;
int working = 1;
while (working == 1) {
int length = 0;
word = calloc((length + 1),sizeof (char));
char c;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n') {
break;
}
length++;
word = realloc(word, length + 1);
word[length - 1] = c;
}
if (strcmp(word, konec) == 0) {
working = 0;
free(word);
break;
}
}
}
我发现很多主题都在讨论同一问题,但我找不到解决方案 anyway.Thanks 来回答您的问题。
问题是您没有在重新分配时添加空终止符:
word = realloc(word, length + 1);
word[length - 1] = c;
此时,word
字符串未终止,因此 strcmp
可能会在搜索空终止符时结束。例如,当您键入 "ko"
时,strcmp
将确定字符 0 和 1 相同,并尝试检查 word[2]
- 您的程序未设置的位置。
添加此行以解决问题:
word[length] = '[=11=]';
当与 konec
的比较不成功时,您还应该将代码添加到 free
word
。
注意:您没有正确使用 realloc
:与其将其分配回 word
,不如将其分配给 temp
,并检查它是否为 NULL
。否则,当 realloc
失败时,您将无法释放先前分配的字。