TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Why my print didn't output before a segmentation fault

38 pointsby yla92over 1 year ago

6 comments

jcalvinowensover 1 year ago
If you use stderr, you get this for free: stderr is always unbuffered (see <a href="https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;3&#x2F;stderr" rel="nofollow">https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;3&#x2F;stderr</a>).
评论 #38830653 未加载
LorenDBover 1 year ago
In C++, I&#x27;ve absolutely had to explicitly use std::endl while debugging to make sure that my debug messages actually were visible when they were supposed to be. Using std::flush also works if you don&#x27;t want to force a newline. However, in my experience, printing a newline character via std::cout does <i>not</i> flush the stream.
评论 #38830756 未加载
评论 #38833783 未加载
Taniwhaover 1 year ago
For those who don&#x27;t know &quot;fflush(0);&quot; flushes ALL FILEs
rini17over 1 year ago
Am I right to claim C compiler could determine the NULL pointer dereference is an undefined behavior and do anything, like completely removing the dereference?
评论 #38829978 未加载
评论 #38830619 未加载
评论 #38834794 未加载
dvhover 1 year ago
Somewhat similar, if you run program in new xterm with -hold parameter and it crashes, you won&#x27;t see the segfault, you have to wrap it in parens.<p>xterm -hold -e &#x27;.&#x2F;a.out&#x27; - no segfault<p>xterm -hold -e &#x27;(.&#x2F;a.out)&#x27; - will print segfault
tyingqover 1 year ago
You can also unbuffer stdout in a wrapper then exec your program. GNU-ish systems, including Linux, usually include a command called stdbuf that does this:<p><pre><code> stdbuf -i0 -o0 -e0 &#x2F;some&#x2F;command </code></pre> Where -i is for stdin, -o is for stdout, and -e is for stderr, and the number is the buffer size.