I'm just learning about malloc() and free() in C and stumbled upon an important question which I couldn't happen to find anywhere. As an example let's take this simple code snippet:
int main(int argc, char *argv[])
{
char *test = malloc(sizeof(char) * 30);
strcpy(test, "test");
FILE *file = fopen("filename", "r");
if (file == NULL){
error("Couldn't open file");
}
free(test);
...
}
How does malloc() and free() now work with this error which just calls exit(EXIT_FAILURE). Because the code doesn't reach free(test) Are there problems if an error occurs and the String test isn't freed? Or does it free automatically when the programm exits?