Conditional jump or move depends on unitialised value(s) when using strcat

58 Views Asked by At

I need to write a program that scans a text file with numbers and strings. I use strtok to separate numbers from strings and than I add the text from file word by word to my string in a structure.

The code works but when i run the program through valgrind it says ==18761== Conditional jump or move depends on uninitialised value(s) ==18761== at 0x483F4B9: strcat (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==18761== by 0x109C31: main (lab.c:225)

So here is the code:

while(fscanf(fi, "%[^\n]\n", str) != EOF) 
{
    word = strtok(str, " \n");
    
    while(word != NULL)
    {
        if((number > 1) && correct) 
        {
            len_text += strlen(word);
            text = realloc(text, (len_text +1) * sizeof(char));
            strcat(text, word);
            
            if(len_text)
            {
                len_text++;
                text = realloc(text, (len_text +1) *sizeof(char));
                strcat(text, " ");
            }
        }

        word = strtok(NULL, " \n");
        number++;
    }
    
    if(len_text) 
    {
        len_text--;
        text = realloc(text, (len_text + 1) * sizeof(char));
        text[len_text] = '\0';
    }
    else 
    {
        correct = 0;
    }

    if (correct) 
    {
        list = realloc(list, (len_list+1) * sizeof(Note));
        (list[len_list]).id = id;
        (list[len_list]).level = level;
        (list[len_list]).text = text;
        len_list++;
    }
    text = NULL;
    len_text = 0;
    correct = 1;
    number = 0;
}
0

There are 0 best solutions below