I want to prevent a memory leak, so I want to free the trie. below you can see my attempt to free up used memory.
static int teller_cleanup = 0;
struct ac {
int value;
char character;
char * word;
struct ac *next;
struct ac *previous;
struct ac *child;
struct ac *parent;
};
this is a double or 4-linked list, not sure that, judging by his words, I
void cleaner(struct ac* a) {
ac * temp = NULL;
if (a != NULL) {
if (a -> child == NULL && a -> next == NULL) {
teller_cleanup ++;
if (a -> parent != NULL) {
temp = a -> parent;
}
else {
temp = a -> previous;
}
free(a -> word);
free(a);
a = temp;
}
if (a -> child != NULL) {
cleaner(a -> child);
}
if (a -> next != NULL) {
cleaner(a -> next);
}
}
}
int cleanup(struct ac* a) {
if (a -> next == NULL && a -> parent == NULL) {
a = a -> child;
}
cleaner(a);
return teller_cleanup;
}
But it looks like it is not working properly. this gives an error:
double free or corrupt (fasttop): 0x0000000000fffa70 ***
what I don't seem to get, because when "child" and "next" are "NULL" than "a", this is the outper most node. And I believe that only one of the recusive if arguments can go to one of these outher most nodes.
I will try to imagine trie:
[root]
|
\/
[h] -- > [b]
| |
\/ \/
[i] [y] --> [e]
trie hi, by be. , . 'h' 'b' , 'h' 'i' .
- , ? .