-2

According to Valgrind, the variable "holder" is uninitialized in my function spaceMove(). Noticeably, it says that whenever I try to manipulate that variable. I tried initializing it to NULL before getting into the loop as well, but it still gives me the same message. It would be much appreciated if someone can explain WHY holder is considered uninitialized, please and thank you.

void spaceMove(char *str, char *delim){
    int i, spPlus=0;
    char *holder;

    holder = malloc(strlen(str)+1);

    for(i=0; i<strlen(str); i++, spPlus++){
        if(str[i] == ' ' && str[i+1] == ' '){
            strcat(holder, " "); //line 11
            //spPlus += 1;
            i += 1;
        }
        else if(str[i] == '\t' && str[i+1] == '\t'){
            strcat(holder, "\t"); //line 16
            i += 1;
        }
        holder[spPlus] = str[i];
    }
    strcpy(str, holder);    //line 21
}

void processStrings(struct mainList *header){
    int i;
    struct stringList *temp;

    temp = header->next;

    while(temp != NULL){
        for(i=0; i<strlen(temp->string); i++){
            if(temp->string[i] == ' ' || temp->string[i] == '\t'){
                spaceMove(temp->string, " "); //line 101
            }
        }           
        temp = temp->next;
    }
}

Here's the Valgrind message:

==3102== Conditional jump or move depends on uninitialised value(s)
==3102==    at 0x4C2F0A8: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3102==    by 0x400B38: spaceMove (listHelper.c:21)
==3102==    by 0x4010CF: Strings (listio.c:101)
==3102==    by 0x4009A4: main (tempMain.c:27)
==3102== 
==3102== Conditional jump or move depends on uninitialised value(s)
==3102==    at 0x400A69: spaceMove (listHelper.c:11)
==3102==    by 0x4010CF: Strings (listio.c:101)
==3102==    by 0x4009A4: main (tempMain.c:27)
==3102== 
==3102== Conditional jump or move depends on uninitialised value(s)
==3102==    at 0x400AC8: spaceMove (listHelper.c:16)
==3102==    by 0x4010CF: Strings (listio.c:101)
==3102==    by 0x4009A4: main (tempMain.c:27)
RGIII
  • 11
  • 4
  • 1
    It's not enough for *variables* to be initialized, you also need to make sure that all *objects* that you access indirectly for reading are initialized. Not every object is a variable. – Kerrek SB Sep 23 '16 at 11:11
  • 2
    I.e. the problem is not the variable is not initialised, the problem is that it points to an uninitialised block of allocated memory. – davmac Sep 23 '16 at 11:17
  • 1
    I would also guess that Valgrid isn't too happy about the blatant memory leak bug that appears each time `spaceMove` is called. You never `free()` the temporarily allocated data anywhere. – Lundin Sep 23 '16 at 11:36
  • This may help you. http://stackoverflow.com/questions/2612447/pinpointing-conditional-jump-or-move-depends-on-uninitialized-values-valgrin – Jay Wai Tan Sep 23 '16 at 14:10

1 Answers1

5

You can not use strcat with a non '\0' terminated string

holder = malloc(strlen(str)+1);
holder[0] = '\0'; /* Add this line */

Or use calloc instead of malloc

David Ranieri
  • 39,972
  • 7
  • 52
  • 94