Search code examples
cmemory-managementmallocheap-memoryvalgrind

Realloc and uninitialized variables (valgrind)


For the life of me, I can't figure out why Valgrind reports the following warnings:

==4988== Use of uninitialised value of size 8
==4988==    at 0x4E62C3F: set_library (mainroutines.c:67)
==4988==    by 0x400E81: main (in /media/src/bin/driver)
==4988==  Uninitialised value was created by a heap allocation
==4988==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4988==    by 0x4E6301F: create_input_data (supportroutines.c:43)
==4988==    by 0x400DAA: main (in /media/src/bin/driver)
==4988== 
==4988== Use of uninitialised value of size 8
==4988==    at 0x4E62C61: set_library (mainroutines.c:68)
==4988==    by 0x400E81: main (in /media/src/bin/driver)
==4988==  Uninitialised value was created by a heap allocation
==4988==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4988==    by 0x4E6301F: create_input_data (supportroutines.c:43)
==4988==    by 0x400DAA: main (in /media/src/bin/driver)
==4988== 

Meanwhile, the set_library function appears like so:

void set_library (Foo* in_data, const int nrows, char* in_string) {
    const int str_length = strlen(in_string);
    char** new_string_array = NULL;

    if (in_data->stored_string==NULL) {
      in_data->stored_string = malloc(sizeof(char*)*1);
    } else {
      new_string_array = realloc(in_data->stored_string, sizeof(char*)*(nrows+1));
      in_data->stored_string = new_string_array;
    };  

    in_data->stored_string[nrows] = malloc(sizeof(char)*str_length);    // first uninitialized warning
    strcpy(in_data->stored_string[nrows], in_string);                   // second uninitialized warning
};

The declaration for in_data->stored_string is char**. I've also checked to make sure stored_string = NULL is done before the set_library function is called. When realloc is not called, I don't seem to get the error. Anyone have ideas what is causing the problem?

EDIT---------------------

D'oh! Opening up the debugger solved this. Actually, the snippet in question had a few problems. I placed the function to initialize values inside the wrong if bracket. Anyways, valid points were raised in the comments, so....

void set_library (Foo* in_data, const int nrows, char* in_string) {
    const int str_length = strlen(in_string)+1;
    char** temp_string_array = NULL;

    temp_string_array = realloc(in_data->stored_string, sizeof(*in_data->stored_string)*(nrows+1));
    in_data->stored_string = temp_string_array;
    in_data->stored_string[nrows] = malloc(sizeof(char)*str_length);    
    strcpy(in_data->stored_string[nrows], in_string);  
}

Solution

  • D'oh! Opening up the debugger solved this. Actually, the snippet in question had a few problems. I placed the function to initialize values inside the wrong if bracket. Anyways, valid points were raised in the comments, so....

    void set_library (Foo* in_data, const int nrows, char* in_string) {
        const int str_length = strlen(in_string)+1;
        char** temp_string_array = NULL;
    
        temp_string_array = realloc(in_data->stored_string, sizeof(*in_data->stored_string)*(nrows+1));
        in_data->stored_string = temp_string_array;
        in_data->stored_string[nrows] = malloc(sizeof(char)*str_length);    
        strcpy(in_data->stored_string[nrows], in_string);  
    }