Originally posted by Lynux-Penguin
The fourth line unallocates the memory that malloc first allocated. this is VERY important otherwise after the program finishes the memory you allocated will stay there until you turn the computer off, and that hogs up memory that you might need later. This is known as a memory leak.
also make sure you use free before changing the pointer at all. You can change the object itself that the pointer points to but don't change the pointer. Don't set it to null, don't point it to something else and don't do ANYTHING until you free() it.
Freeing unused memory is a good idea, but it's not mandatory. When your program exits, any memory which it has allocated but not freed should be automatically released. If your computer were to somehow ``lose'' memory just because your program forgot to free it, that would indicate a problem or deficiency in your operating system