I want to return a pointer to the head structure of linked list in following function but since my fread can't read struct's string, I have to malloc some variables and point newheader->name string to it. In following function if I free my temporary variables then returned new header only prints null values and If I don't then I get memory leaks but function works fine. I want to be able to return head of a complete linked list not null value.
struct head *read_strings(char *file) {
FILE *my_file;
struct head * newheader = NULL;
char *str = NULL;
newheader = buildhead();
int len, lenStr, lenTotal = 0;
printf("\n\n");
my_file = fopen(filename, "rb");
if (my_file == NULL) {
printf("error\n");
}
fread(&len,sizeof(int),1, my_file);
newheader->name = malloc(len);
fread(newheader->name,sizeof(char),len, my_file);
fread(&newheader->length,sizeof(int),1, my_file);
if (newheader->length != 0) {
if (newheader->length == lenTotal) {
fread(&lenStr,sizeof(int),1, my_file);
str = malloc(lenStr); //char *str = malloc(lenStr);
fread(str,sizeof(char),lenStr, my_file);
create_string(newheader, str);
lenTotal += lenStr;
str = NULL; //free(str);
}
else {
while (newheader->length != lenTotal) {
fread(&lenStr,sizeof(int),1, my_file);
str = malloc(lenStr); //char *str = malloc(lenStr);
fread(str,sizeof(char),lenStr, my_file);
create_string(newheader, str);
lenTotal += lenStr;
str = NULL; //free(str);
}
}
}
printString(newheader);
fclose(my_file);
free(str);
//if i free newheader->name here then I get no memory leaks
//free(newheader->name);
freeStructure(newheader);
return newheader;
}
I am required to read strings from binary file and store them into struct. But I can't store values directly to string variable of struct unless I malloc a new string and point to it. You can see in above code that I am able to read newheader->length from fread but not newheader->name. I tried to put fread results in a string array but got segmentation fault.
This is how my binary file looks like. It has null terminator after every string and int.
0000000 021 \0 \0 \0 i t i s a g o o d
0000020 d a y \0 R \0 \0 \0 # \0 \0 \0 I t
0000040 w a s t h e b e s t o f
0000060 o y e h o y e t i m e s . \0
0000100 034 \0 \0 \0 I t w a s t h e b
0000120 l u r s t o f t i m e s . \0
0000140 \a \0 \0 \0 m o n k e y \0 006 \0 \0 \0 p
0000160 a n d a \0 006 \0 \0 \0 s h i f u \0
0000177
If I free newheader->name in the my freestruct() function then I get following error
*** Error in `./test': munmap_chunk(): invalid pointer:
0x000000000040133e *** Aborted.
if don't free it then I lose 17 bytes in 1 block.
==24169== HEAP SUMMARY:
==24169== in use at exit: 0 bytes in 0 blocks
==24169== total heap usage: 5 allocs, 6 frees, 1,201 bytes allocated
==24169==
==24169== All heap blocks were freed -- no leaks are possible
==24169==
==24169== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from0)
==24169==
==24169== 1 errors in context 1 of 1:
==24169== Invalid free() / delete / delete[] / realloc()
==24169== at 0x4C29E90: free (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==24169== by 0x401298: freestruct (A1.c:267)
==24169== by 0x400F8A: write_strings (A1.c:189)
==24169== by 0x400840: main (test.c:25)
==24169== Address 0x40133e is not stack'd, malloc'd or (recently)
free'd
==24169==
==24169== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from0)
This is small piece of code from my freestruct() function
if (header->next == NULL) {
header->length = 0;
free(header->name);
free(header);
}
After writing string to binary file, I freed the whole structure and then created again in read_strings function
Edit: If I free newheader->name inside readString function then I get no memory leaks but I want to be return newheader to main and call printString and freestruct after that.
I apologize for my bad grammar.
setName
, what does it do? And what doesgetName
do? Do you need to set the structuresname
member twice? – Some programmer dude