This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

segmentation fault with fclose


Hi,

I'm trying to close a file using fclose, but the following program seg-faults:


#include <stdio.h>

int main(void)
{
    FILE* fp = fopen("unfounded.txt", "r");

    if(fp == NULL)
        fclose(fp);

    return 0;
}


The file I'm trying to open doesn't exist in the directory, so fp is
indeed NULL. So it's just fclose(NULL).
Is fclose(NULL) legal according to the C standard ? As an end-user I
expect fclose(fp) to close the file (if it is already open) or just do
nothing (if it is not open). So the seg fault -- is this a compiler
bug ?

PS: I hope this is the right place to ask this question.


-- Satish.BD


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]