Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

fclose(NULL)

1,149 views
Skip to first unread message

Chris Hills

unread,
May 2, 2000, 3:00:00 AM5/2/00
to
Hi all,

Today I was asked what will fclose(NULL) do?

Microsoft Visual C/C++ V6 (used as a C compiler) generates an
exception (presumably because it tries to dereference the NULL
pointer)!

I think it should just return an error result (EOF, as defined in K & R
for errors during fclose() calls).

What do you think?
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\ Chris Hills Staffs /\/\/\/\/\/
/\/\/\/\/\/\/\/\/\ England /\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Paul Jarc

unread,
May 2, 2000, 3:00:00 AM5/2/00
to
Chris Hills <ch...@phaedsys.demon.co.uk> writes:
> Today I was asked what will fclose(NULL) do?

n869 7.19.5.1p2:
# The fclose function causes the stream pointed to by stream to be
# flushed and the associated file to be closed.

No mention is made of what happens when `stream' doesn't point to a
stream, so I'd say it's implicitly undefined behavior.


paul

James Kuyper

unread,
May 2, 2000, 3:00:00 AM5/2/00
to
Chris Hills wrote:
>
> Hi all,

>
> Today I was asked what will fclose(NULL) do?
>
> Microsoft Visual C/C++ V6 (used as a C compiler) generates an
> exception (presumably because it tries to dereference the NULL
> pointer)!
>
> I think it should just return an error result (EOF, as defined in K & R
> for errors during fclose() calls).
>
> What do you think?

Section 7.1.4 p1 is an overview that applies to all the standard library
functions. It says that "If an argument to a function has an invalid
value (such as ... a null pointer) ... , the behavior is undefined."
Obviously, this doesn't apply to functions which have specifically
defined behavior for null pointers, but that's not the case with
fclose().

David R Tribble

unread,
May 5, 2000, 3:00:00 AM5/5/00
to
James Kuyper wrote:

It's interesting to note, however, that fflush(NULL) is defined and
causes all open streams to be flushed.


--
David R. Tribble, mailto:da...@tribble.com, http://david.tribble.com

James Kuyper

unread,
May 5, 2000, 3:00:00 AM5/5/00
to

You're right. There's quite a few standard library routines with defined
behavior for a null pointer argument. In fact, there's too many to make
it worthwhile listing them. However, fclose() isn't one of them.

0 new messages