I´ve been always told to free the heap memory allocated by malloc()
:
#include <stdlib.h>
#define width 5
int main(void)
{
char* ptr = malloc(sizeof(*ptr) * width);
/* some stuff with the heap object */
free(ptr);
return 0;
}
But now I´ve read in What REALLY happens when you don't free after malloc? that I don´t have to because the Operation System will automatically release the occupied memory after the program is terminated.
But why did my teacher told me to do so then? Is there any benefit of doing so?