简体   繁体   English

断言前的printf不起作用

[英]printf before assert doesn't work

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. 在调试过程中,我发现assert之前的所有printf都无法正常工作。 They're simply not printed most of the time. 大多数情况下,根本不会打印它们。 I tried adding fflush(stdout ) but it doesn't seem to help. 我尝试添加fflush(stdout ),但似乎无济于事。

Any other thoughts or alternatives? 还有其他想法或选择吗?

Example: 例:

printf... <- not printed
printf... <- not printed due to the assert. stdout not flushed?

do something

assert()

Call fflush(stdout) before assert . assert之前调用fflush(stdout) 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. 或者,如果尚未重定向stdout并引用终端,则只需在消息末尾写换行符就足够了。 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) or fflush is encountered. 默认情况下,stdout会被缓冲(行缓冲在终端上;否则全缓冲),因此直到输出缓冲区溢出或换行(在行缓冲模式下)或出现fflush之前,才会实际写入输出。

您是否正在进行优化编译-因为printfs / assert的实际顺序可能不符合您的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM