Fclose (), then free ()?

Suppose I have the following program:

#include <stdio.h>

int main () {
    FILE * pFile;
    pFile = fopen ("myfile.txt","r");

    fclose (pFile);
    //This never happens: free(pFile)
    return 0;
}

I have never seen a program that does free(pFile)after closing a file descriptor. Why is this?

I know that since it fclose()does not receive a pointer to pFile, it does not actually free up pointer memory. I got the impression that pointers should always free up their memory if they point to dynamically allocated memory. Why does anyone free()need a file pointer?

+7
source share
4 answers

free malloc . fopen, , mallocing, (fclose), , fopen. fopen , .

alloc free. , alloc, , .

, fclose , fopen.

+15

fopen ( ). , fclose , fopen.

+4

When closing a file more than once, fclosereturns 0 the first time, and then -1.

Therefore, it is likely to take care of this in a reasonable way.

At least this is the case on Windows, please correct me if I am wrong.

0
source

fclose () will free related resources. However, when you finish the fclose (fp); command, fp is still not NULL. Therefore, you need to explicitly set it to NULL.

0
source

All Articles