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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

'Undefined behavior' doesn't exist

2 点作者 leoncaet大约 3 年前

3 条评论

Someone大约 3 年前
&gt; I have never seen even a single example of &quot;undefined behavior&quot; that I thought was justified. All &quot;undefined behavior&quot; that I have seen could have been replaced with &quot;the compiler can do one of these three things&quot; and still been widely efficient<p>So, what’s that list for “write to a memory location outside allocated memory”?<p>That may corrupt some internal structure for a hard-disk, causing a loss of the entire file system if data is later flushed.<p>Worse, with memory-mapped I&#x2F;O, that can trigger a change to an I&#x2F;O pin, so it could conceivably launch an ICBM, starting world war three.<p>The best one probably could do “it may or may not perform that write, and the write may have arbitrary effects, not necessarily immediately”. Is that truly better than <i>“undefined behavior”</i>?
josephcsible大约 3 年前
&gt; All &quot;undefined behavior&quot; that I have seen could have been replaced with &quot;the compiler can do one of these three things&quot; and still been widely efficient<p>The point of undefined behavior is that it&#x27;s a list of things that the compiler is allowed to assume that you&#x27;ll never do. A lot of optimizations come from being allowed to make this assumption and propagate it backwards, which can&#x27;t be done at all if the compiler would have to do one of a defined list of things instead.
tinalumfoil大约 3 年前
&gt; I have never seen even a single example of &quot;undefined behavior&quot; that I thought was justified<p>That’s the whole point. The compiler doesn’t have to justify itself on undefined behavior.<p>And no example that was justified? What about a compiler that turns dereferencing a null pointer into a memory access to address 0? Seems like a pretty reasonable translation to me.