我不明白为什么有时需要使用 fflush()
有时不需要。
目前我的程序出现段错误,我正在使用 print 语句对其进行调试。当程序出现段错误时,stdout
不会自动刷新其缓冲区吗?
最佳答案
I don't understand why sometimes I need to use fflush() and sometimes not.
有时 stdio
缓冲区会被刷新,有时不会。例如,在打印的内容中简单地包含一个“\n”通常会刷新它(因为 stdout
在连接到终端时默认是行缓冲的)。
When a program segfaults, does stdout not flush its buffer automatically ?
Stdio 缓冲区由 exit
刷新。当信号(例如 SIGSEGV
)终止进程时,不会调用 exit
。在不刷新 stdio
缓冲区的情况下退出进程的另一种方法是使用特定于 Unix 的调用 _exit
。
关于c - 没有 fflush(stdout) 输出不打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8316261/