I've been working on a project that uses structs as storage for strings. I declared a struct consists of char type members:
struct datastore1 { char name[50]; char address[50]; char email[50]; char number[50]; char idnum[50]; }; I'm aware that I can just do char *name, char *address... but let's say we specified it with max length of 50. Then on my function which uses the struct, I malloc'ed it with index size of 30:
struct datastore1 *dsdata = malloc(30 * sizeof(struct datastore1)); Supposedly I finished copying all strings into the struct by accessing each index, How should i free the allocated memory that was used after calling malloc? I tried doing free(dsdata) on the end of the program but I am not sure if it's the right way. Should i free each indexes individually? Please enlighten me. Thank you in advance for the feedback!



free(dsdata)is correct. It's actually relatively simple - each malloc should have exactly one free of the same address that the malloc returned.struct datastore1 *dsdata = malloc(30 * sizeof(*dsdata));