I am working on a hashtable data structure, and I've come across a problem with a memory leak. Basically I have this code:
int main(int argc, char const *argv[])
{
// -----------------------------+
// record 0 |
// -----------------------------+
rec_t *r = malloc(sizeof(rec_t));
r->key = "name";
r->value = "evgeny";
// -----------------------------+
// -----------------------------+
// record 1 |
// -----------------------------+
rec_t *r2 = malloc(sizeof(rec_t));
r2->key = "lastName";
r2->value = "Danilenko";
// -----------------------------+
// -------------------------------+
// dictionary struct |
// params : uint length |
// rec_t **records |
// -------------------------------+
dct_t* dict = malloc(sizeof(dct_t));
dict->records = malloc(sizeof(rec_t*) * 20);
dict->records[0] = r;
dict->records[1] = r2;
// free(dict->records[0]); // DONT WANT TO DO THIS! O(n)
// free(dict->records[1]);
free(*dict->records); // clears only 1st element cuz pointer
free(dict->records);
free(dict);
return 0;
}
How would I got about free()'ing all of the elements with 'one shot'? I had a look a this link but it didn't help - How to free a struct that contains only pointers
I want to emphasise that I don't want to loop through all the elements and freeing the one-by-one.