我专门在 C11 标准 (ISO/IEC 9899:2011) 或 POSIX 页面中查找该单词。我检查了POSIX manual for fwrite出,但它根本没有提到未定义的行为。然而,manual for fclose确实这么说
After the call to fclose(), any use of stream results in undefined behavior.
但我仍然不知道 NULL
流是否会导致 UB。
最佳答案
fread()
和 fwrite()
都期望 stream 参数是成功调用 fopen 返回的值()
、fdopen()
或 freopen()
。由于这些函数在出错时返回 NULL,因此 stream 参数不能为 NULL。
由于手册页没有说明当stream不是成功调用fopen()
等返回的值时会发生什么,这意味着手册页确实不要说当stream不是成功调用fopen()
等返回的值时会发生什么,或者换句话说,行为未定义。
另请参阅此板上对问题“Why glibc's fclose(NULL) cause segmentation fault instead of returning error”的已接受答案。
关于posix - 当流为空时,fwrite 和 fread 等操作是否具有未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45924641/