You should only free the memory you allocate when you're all done with it. In your case, that would be after the 2nd printf, on line 37. Calling free doesn't erase the contents, but it does free up...