After I fixed the memory leak, valgrind shows me a bunch of lines errors as the following, and I have no idea how to fix that. Is it because I free more space than I need or something?
line 39:
root = importTree(*(argv+1));
line 72:
printf("Result found for %d:\n city: %s\n state:%s\n", zip, new->city, new->state);
Node *importTree(char *filename) {
Node *root = NULL;
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("Error opening file.\n");
return NULL;
}
while (!feof(fp)) {
Node *new = malloc(sizeof(Node));
if (!new) {
printf("Failed to allocate memory. Ending read.\n");
exit(1);
fclose(fp);
}
new->city = malloc(sizeof(char) * MAXCITYNAME);
if (!(new->city)) {
printf("Failed to allocate memory. Ending read.\n");
exit(1);
fclose(fp);
}
new->left = NULL;
new->right = NULL;
char *line = malloc(sizeof(char) * MAXLINELENGTH);
if (!line) {
printf("Failed to allocate memory. Ending read.\n");
exit(1);
fclose(fp);
}
if (fgets(line, MAXLINELENGTH, fp) == NULL) {
if (!feof(fp)) {
printf("File reading ended prematurely. Check for errors in the file.\n");
exit(1);
fclose(fp);
}
free(new->city);
free(line);
free(new);
fclose(fp);
break;
}
char *tmp = strtok(line, ",");
new->zipCode = atoi(tmp);
tmp = strtok(NULL, ",");
strcpy(new->city, tmp);
new->city[strlen(tmp) + 1] = '\0';
tmp = strtok(NULL, ",");
strcpy(new->state, tmp);
new->state[2] = '\0';
root = addNode(root, new);
if (!root) {
printf("Root of tree is still NULL! Ending read.\n");
exit(1);
}
free(line);
free(new->city); \\this is the line 220
}
fclose(fp);
return root;
}
Here is valgrind's output:
Invalid read of size 1
==7879== at 0x517FAB4: vfprintf (vfprintf.c:1635)
==7879== by 0x5188C98: printf (printf.c:34)
==7879== by 0x400BBD: main (hw3.c:72)
==7879== Address 0x5657a30 is 0 bytes inside a block of size 30 free'd
==7879== at 0x4C2AD17: free (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==7879== by 0x40103F: importTree (hw3.c:220)
==7879== by 0x400A31: main (hw3.c:39)
==7879==
==7879== Invalid read of size 1
==7879== at 0x51ADA99: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1342)
==7879== by 0x517FA6C: vfprintf (vfprintf.c:1635)
==7879== by 0x5188C98: printf (printf.c:34)
==7879== by 0x400BBD: main (hw3.c:72)
==7879== Address 0x5657a37 is 7 bytes inside a block of size 30 free'd
==7879== at 0x4C2AD17: free (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==7879== by 0x40103F: importTree (hw3.c:220)
==7879== by 0x400A31: main (hw3.c:39)
==7879==
==7879== Invalid read of size 1
==7879== at 0x51ADAAC: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1342)
==7879== by 0x517FA6C: vfprintf (vfprintf.c:1635)
==7879== by 0x5188C98: printf (printf.c:34)
==7879== by 0x400BBD: main (hw3.c:72)
==7879== Address 0x5657a36 is 6 bytes inside a block of size 30 free'd