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 /\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
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
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().
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
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.