TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

What Every C Programmer Should Know About Undefined Behavior #1/3

155 点作者 zeugma大约 14 年前

6 条评论

aidenn0大约 14 年前
I still remember one of my favorite linux bugs was due to the NULL behavior.<p>It was roughly this<p>read-from-p;<p>if(p != NULL) write-to-p;<p>since read-from-p is undefined if p is null, gcc could (and did) legally optimize out the NULL check, so you could end up writing to NULL.<p>[edit] I noticed that this case of bug is actually mentioned in the regehr article that is linked.
评论 #2540498 未加载
评论 #2541124 未加载
jergosh大约 14 年前
"You don't know. The computer may levitate." is what they used to say in my C course at the uni.
评论 #2542130 未加载
caf大约 14 年前
<i>It is worth noting that unsigned overflow is guaranteed to be defined as 2's complement (wrapping) overflow, at least by Clang and other commonly available C compilers.</i><p>This is actually guaranteed by the standard (although the language used is not "2s complement", but "repeatedly adding or subtracting one more than the maximum value that can be stored in the type until the value is in range").<p><i>C requires that these sorts of type conversions happen through unions: using pointer casts is not correct and undefined behavior results.</i><p>Actually, loading a member of a union other than the one most recently stored to is undefined behaviour too - it's just that using a union in this way falls into the <i>"...both Clang and GCC nail down a few behaviors that the C standard leaves undefined."</i> category. (And most other compilers besides - there is much historical usage of this).
brimpa大约 14 年前
It would be nice to have a code snippet for each of the examples. I'm a fairly experienced C++ developer but my knowledge of compilers is, admittedly, lacking and I just want to make sure I'm on the same page.<p>Otherwise, a great read.<p>PS– Is it just me or is LLVM coming up more and more these days?
评论 #2540726 未加载
olov大约 14 年前
Great article, shame it got optimized away before I had the chance to re-read it.<p>edit: seems Blogger turned their -O knob to eleven <a href="http://status.blogger.com/" rel="nofollow">http://status.blogger.com/</a>
KonradKlause大约 14 年前
Why does LLVM generate "ud2" instructions? WTF?
评论 #2540673 未加载