My program is reading a file (test.txt and it contains only 2 strings and a white space like : "Hello World") and when I use calloc, it gives me memory leak with using valgrind. The problem is I get more bytes lost in second mem allocation(b = calloc(11,sizeof(*b)).
I have tried to use free() it didn't work
char str[1024];
char *a = NULL;
char *a = NULL;
int i = 0;
while(!feof(myfile)) {
//I used some codes here to skip "\r\n" which is working fine.
fscanf(myfile, "%10s", str);
i = strlen(str);
if(key_find(k,str) == NULL){
a = calloc(i,sizeof(*a));
strcpy(a,str);
key_insert(k,a);
}
fscanf(myfile, " ");
fscanf(myfile, "%10s", str);
if(key_find(k,str) == NULL){
b = calloc(i,sizeof(*a));
strcpy(b,str);
key_insert(k,a);
}
}
free(a); free(b);
It does not give me any memory leak when I only have 2 different strings in my txt file. But if I have more than 4 strings then it gives me memory leak.