Search code examples
cmallocvalgrindrealloc

C Valgrind - Conditional jump depends on unitialitialised value


i want to repair one error..

Valgrind says me this:

==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)

Here is my function 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;

value text is given to function from main. Problem is somewhere in strncat

Thans you!! Lukas


Solution

  • You never initialize the contents of textNovy, yet you concatenate on to the end of it. This leads to the error you are seeing from valgrind.

    You need at least:

    textNovy[0] = '\0';
    

    (or an equivalent) after the malloc().