我想修复一个错误..
Valgrind这样说:
==9203== 1 errors in context 1 of 1:
==9203== Conditional jump or move depends on uninitialised value(s)
==9203== at 0x4C2D64A: strncat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9203== by 0x400970: newSpeak (main.c:39)
==9203== by 0x400A62: main (main.c:74)
==9203==
--9203--
--9203-- used_suppression: 2 dl-hack3-cond-1
==9203==
==9203== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
这是我的函数newSpeak()
int velikost = 0, i = 0, delka = 0;
char * textNovy = NULL;
i = 0;
while (text[i] != '\0') {
delka++;
i++;
}
textNovy = (char*)malloc(sizeof(char));
for (i = 0; i < delka; i++) {
textNovy = (char*)realloc(textNovy, ((i+1)+velikost)*sizeof(char) );
strncat(textNovy, text+i, 1);
}
return textNovy;
值文本被赋予main的功能。 问题出现在strncat中
你呢!卢卡斯
答案 0 :(得分:4)
你永远不会初始化textNovy
的内容,但你会连接到它的结尾。这会导致您从valgrind
看到的错误。
至少需要:
textNovy[0] = '\0';
(或等效的)malloc()
之后。