AFGhazy's blog

By AFGhazy, history, 9 years ago, In English

how can I free all the memory used by a trie ?!

  • Vote: I like it
  • -1
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it +1 Vote: I do not like it

memset(t, 0, sizeof t);

»
9 years ago, # |
  Vote: I like it +1 Vote: I do not like it

How you implements a trie?

»
9 years ago, # |
  Vote: I like it +17 Vote: I do not like it

Well, it certainly depends on the implementation. If your trie is something like this:

class trie{
    map<char, trie> M;
};
trie Root;

Then you don't need to free anything. Though Root.M.clear() does the thing. On the other hand, if it looks like this:

class trie{
    ~trie(){
        forn(i, 256) if( trie[i]) delete trie[i];
    }
    trie *[256];
};

Always remember to implement a destructor if you don't want to have memory leaks.

And lastly, if you used indexes instead of pointers, feel free to use a memset(trie, 0, sizeof(trie)), but bear in mind this won't free the memory but zero its content. (You can now safely overwrite it), :)