This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
segmentation fault with fclose
- From: bd satish <bdsatish at gmail dot com>
- To: gcc at gcc dot gnu dot org
- Date: Mon, 21 Mar 2011 00:47:39 +0200
- Subject: 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