I have three files
struct.h struct.c main.c
struct.h contains declaration of structs and some functions
struct.c contains global variable bglobal an instance of struct b and function implementations which use bglobal. It includes .h file
main.c call some of the functions declared in struct.h. It also includes .h file
struct.h contains two struct
struct a{
int *s
}
struct b{
struct a* arr
}
void init();
void more();
struct.c file
#include"struct.h"
struct b bglobal;
void init(){
bglobal.arr = malloc(sizeof(struct a)*5);
}
void more(){
*bglobal.arr[0].name = 'I';
}
main.c file
#include "main.h"
int main(){
init();
more();
}
I want that at end of program memory allocated to bglobal.arr get freed up.
Using valgrind it says some bytes still reachable.
How to achieve this?
free(bglobal.arr)?init()that frees the allocated memory when the program exits. Or provide an uninit() function that the client code can call themselves to free the resources.atexitfrom yourinitfunction.