未定义的行为 fopen、fclose

Undefined behavior fopen, fclose

以下代码会导致未定义的行为吗?

FILE *fp;
fopen_s(&fp, "abc.bin", "rb");
fclose(fp);
fclose(fp); // accidentally closed an already closed file.

我知道在已释放的数组上调用 free 会导致 UB。因此我问。

引自man fclose

The behaviour of fclose() is undefined if the stream parameter is an illegal pointer, or is a descriptor already passed to a previous invocation of fclose().

所以是的,这是未定义的行为。