断言之前的 printf 不起作用
我想我以前见过这个问题,我打赌那里有更好的解决方案,所以问。
在调试过程中,我发现 assert
之前的任何 printf
都不能很好地工作。大多数时候它们根本不被打印出来。我尝试添加 fflush(stdout) 但似乎没有帮助。
还有其他想法或替代方案吗?
例子:
printf... <- not printed
printf... <- not printed due to the assert. stdout not flushed?
do something
assert()
I think I've seen this issue before and I bet there's better solution out there so asking..
During debugging I found that any printf
before assert
don't work well. They're simply not printed most of the time. I tried adding fflush(stdout
) but it doesn't seem to help.
Any other thoughts or alternatives?
Example:
printf... <- not printed
printf... <- not printed due to the assert. stdout not flushed?
do something
assert()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
assert
之前调用fflush(stdout)
。或者,如果 stdout 尚未重定向并引用终端,则只需在消息末尾写入换行符就足够了。默认情况下,stdout 是缓冲的(在终端上进行行缓冲;否则是完全缓冲),因此在输出缓冲区溢出或遇到换行符(在行缓冲模式下)或遇到 fflush 之前,实际上不会写入输出。Call
fflush(stdout)
beforeassert
. Or, if stdout has not been redirected and refers to the terminal, just writing a newline at the end of your message should be sufficient. By default, stdout is buffered (line buffered on terminals; fully buffered otherwise) and thus output will not actually be written until the output buffer overflows or a newline (in line-buffered mode) orfflush
is encountered.您是否进行了优化编译 - 因为 printfs/assert 的实际顺序可能不是您所期望的。
Are you compiling with optimization - because the actual order of the printfs/assert may not be what you expect.