I understand that not closing a file is irresponsible, but under the following two conditions, what issues can arise if fclose()
was not be called?
1) In the event that fclose() was not called when a program ends, does the OS communicate with the file descriptor to let it know that the file is not longer needed and closes it?
#include <stdio.h>
int main(){
FILE* f = open("sample.txt","r");
return 0;
};
In the event of a explicit exit e.g. exit(1)
does it differ in any way as to just returning in the previous example?
#include <stdio.h>
int main(){
FILE* f = fopen("sample.txt","r");
exit(1);
return 0;
}
2) A potential segmentation fault occurs before fclose()
could be called?
#include <stdio.h>
int main(){
FILE* f = fopen("sample.txt","r");
int *ptr = NULL;
*ptr = 1; //seg-fault
fclose(f);
return 0;
};