Generally modern operating systems free any memory left that the program is not using, but is still accessible. However, if you overwrite an old pointer, there is no way that the operating system could catch that.
To illustrate the difference, I wrote two quick programs.
This one has memory that the OS can recover:
#include <stdlib.h>
int main(void) {
char *a = malloc(10);
return 0;
}
This one has memory that the OS cannot recover: #include <stdlib.h>
int main(void) {
char *a = malloc(10);
a = malloc(10);
return 0;
}