Originally posted by: mattg1981
I just compiled it using DWW's method of
free(words[k].word);
words[k].word = NULL;
and that worked too ... so let me see if I have this right:
free(words[k].word) -> frees or unmalloces the memory that I owned .. (what exactly does this mean though? ... did I just kill the pointer to it or did I erase what was in that memory address or did I just tell the OS .. "HERE you go, I dont need it anymore")
and then the words[k].word = NULL ... I dont know what I need this really .. I just freed it in the line above ... could you explain this one please![]()
As long as you use "free" for each piece that you use "malloc" for, then there will be no memory leaks. Remember that. For every "malloc" there should be a corresponding "free" no matter what.
YES. free(words[k].word) frees the memory that you allocated (don't say unmallocates its not a word
For example, RIGHT after you free it...the pointer would still point to that piece of memory but you would no longer own it (the environment takes care of giving it back to the system). but if you tried to access that piece of memory through your pointer it would be a WRONG thing. You don't own it and you don't know what it points to anymore...something else is probably using it by that point in time. That is why you must set the old pointer to that data to NULL because you don't own that memory chunk. Got it?
the words[k].word = NULL line is important just because you want to avoid any possible problem (like in the future if you accidently tried to access what it points to, NULL, the sytem would know that doesn't exist and prevent you). If you tried to access the piece of memory that the pointer points to (and you didn't set the pointer to NULL) the system WONT STOP YOU. That is why it is dangerous.
Moral of the story: don't point to anything you don't own
