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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Undefined behavior in C is a reading error

9 点作者 r4um大约 4 年前

2 条评论

dgrunwald大约 4 年前
Unfortunately you have to choose what you want your C compiler to be. It can be little more than a macro assembler that preserves &quot;the obvious&quot; behavior of undefined constructs on the target platform. Most compilers will do this by default (-O0).<p>Or you can opt into using an optimizing compiler (-O2). But when optimization passes ignore the possibility of undefined behavior with unpredictable results (as explicitly allowed by the standard), you get, well, unpredictable results.<p>Nowadays compilers even offer the &quot;terminating a translation or execution&quot; variant that the standard proposes (-fsanitize=undefined,address). So really, the choice is up to you.<p>But what people really want is of course: keep the undefined behavior that <i>my</i> program happens to use predictable, but given that constraint, optimize as much as possible (=exploit all the <i>other</i> undefined behavior).<p>Example: inlining functions is an acceptable optimization, because of course I&#x27;m not intentionally using out-of-bounds pointers to grab the return address from the stack.
评论 #27222748 未加载
评论 #27233346 未加载
pjmlp大约 4 年前
&gt; We have the absurd situation that C, specifically constructed to write the UNIX kernel<p>Actually it was to <i>re-write</i> UNIX kernel, small but very relevant distinction.