I’m getting an error in valgrind and don’t know what is wrong.
The error is:
valgrind output:
==1112== Conditional jump or move depends on uninitialised value(s) ==1112== at 0x402BF0D: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
And it states the problem occurs on line 226:
if(reallocate ==TRUE)
{
char** temp_values = NULL;
temp_values = (char**) realloc(theBoard->_values, theBoard->_size_r*sizeof(char*) );
if(temp_values!=NULL)
{
theBoard->_values = temp_values;
} else
{
reportError(MEM_OUT);
return FALSE;
}
int i = 0;
for (i=0; i<theBoard->_size_r; i++)
{
char* temp_values_c = NULL;
HERE( line 226)-> temp_values_c = realloc(theBoard->_values[i], theBoard->_size_c*sizeof(char) );
if(temp_values_c != NULL)
{
theBoard->_values[i] = temp_values_c;
} else
{
reportError(MEM_OUT);
return FALSE;
}
}
// initialize extra memory
if(row_out_bound)
{
init(theBoard,prev_size_r,0);
}
if(col_out_bound)
{
init(theBoard,0, prev_size_c);
}
}
Why is this happening and how can i fix it?
The problem is
theBoard->_values[i]is not initialized since it comes straight fromrealloc(temp_values = ...).EDIT
I thought you’d never ask. The function
reallocreturns a chunk of memory of the specified size, with no guarantees regarding its contents. So for all practical purposes you should assume anythingreallocreturns contains garbage. In your code you take that (potential) garbage and on line226you tell realloc:Here’s this pointer that’s like totally valid and all. It’s
NULLor I previously obtained it frommalloc. Can you realloc it to this size? And that’s not true! The actual value contained bytheBoard->_values[i]could be anything.What you want is a loop that does
theBoard->_values[i] = NULL, or maybe usemallocinstead ofreallocon line 226.