I am trying to use valgrind
to detect memory errors.
This is part of my code-
else
{
137. printf("HELlo\n");
138. char * lexeme1;char * lexeme2;
139. lexeme1=substr1(bufferOld,beginPointer,sizeBuffer-1);
140. lexeme2=substr1(buffer,0,indexStart-1);
141. strcat(lexeme,lexeme1);
142. strcat(lexeme,lexeme2);
}
Token getNextToken( int fp1, FILE * fp)
{
...
207. lexeme=(char *)malloc(sizeof(char) * 100);
...
}
Upon running valgrind
it gives me the following error-
==9720== Conditional jump or move depends on uninitialised value(s)
==9720== at 0x4C2DD9A: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9720== by 0x401048: updateToken (lexer.c:141)
==9720== by 0x402A92: getNextToken (lexer.c:498)
==9720== by 0x400A17: main (driver.c:66)
==9720== Uninitialised value was created by a heap allocation
==9720== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9720== by 0x4012C6: getNextToken (lexer.c:207)
==9720== by 0x400A17: main (driver.c:66)
==9720==
I am not sure why I am getting these kinds of error. Any help would be highly appreciated.
Update-
Here is my substr1 function-
char * substr1(char * source,int start,int end)
{
char * dest=malloc((end-start+2)*sizeof(char));
if(end==-1)
return dest;
int i,count=0;
for(i=start;i<=end;i++)
dest[count++]=source[i];
dest[count]='\0';
return dest;
}