Another C question:
let's say I have a struct that has a pointer member of char*
type.
When I want to initialize an instance of the struct I call malloc
:
MyStruct* ptr = (MyStruct*)malloc(sizeof(MyStruct)
And then allocate 256 bytes of memory for the char*
member:
ptr->mem = (char*)malloc(sizeof(char)*256);
what happens to the pointer member and the memory it points to when I call
free(ptr);
?
when I check the program with valgrind I see that I have a memory leak, but when I explicitly call free(ptr->member);
I still have a memory leak and valgrind shows an "Invalid free" error
What's the proper way the manage the memory pointed by the member?