In Unix C programming, is it considered good practice to explicitly close file handles before the process exits, or is it instead good practice to let the OS close the file handles and thus avoid unnecessary code?
Which of the two would generally be considered as the preferred alternative?
Example:
int main (int argc, char* argv[]) { int sd; sd = socket(...); // Snip close(sd); // Good or bad practice? return 0; }
Generally the code that’s doing the opening and closing doesn’t know if the process is going to immediately exit – so it’s best to include explicit code.
Even if the code is residing on the top-level main() function, it would still be a good idea in case the code is ever re-used.