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.

PLOS2021: ISO-C became unusable for operating systems

60 pointsby vyodaikenover 3 years ago

9 comments

pcwaltonover 3 years ago
In general I&#x27;ve long been very skeptical of removing optimizations that rely on undefined behavior. People say &quot;I&#x27;d happily sacrifice 1% for better theoretical semantics&quot;, but theoretical semantics don&#x27;t pay the bills of compiler writers. Instead, compiler developers are employed by the largest companies, where a 1% win is massive amounts of dollars saved. Any complaint about undefined behavior in C <i>must</i> acknowledge the underlying economics to have relevance to the real world.<p>As the paper notes, there are plenty of alternative C compilers available to choose from. The reason why GCC and LLVM ended up attaining overwhelming market share is simply that they produce the fastest possible code, because, at the end of the day, <i>that is what users want</i>.<p>If you want to blame someone, blame the designers of the C language for doing things like making int the natural idiom to iterate over arrays even when size_t would be better. The fact that C programmers continue to write &quot;for (int i = 0; i &lt; n; i++)&quot; to iterate over an array is why signed overflow is undefined, and it is absolutely a critical optimization in practice.
评论 #28780162 未加载
评论 #28780206 未加载
评论 #28783538 未加载
评论 #28785533 未加载
tialaramexover 3 years ago
A bad workman blames his tools, so they say.<p>There is a large population of C &quot;real programmers&quot; who, when they write a C program that unsurprisingly doesn&#x27;t work, conclude this must be somebody else&#x27;s fault. After all, as a real programmer they certainly meant for their program to work, and so the fact it doesn&#x27;t can&#x27;t very well be their fault.<p>Such programmers tend to favour very terse styles, because if you don&#x27;t write much then it can&#x27;t be said to be your fault when, invariably, it doesn&#x27;t do what you intended. It must instead be somebody else&#x27;s fault for misunderstanding. The compiler is wrong, the thing you wanted was the obviously and indeed only correct interpretation and the compiler is <i>willfully</i> misinterpreting your program as written.<p>Such programmers of course don&#x27;t want an error diagnostic. Their program doesn&#x27;t have an error, it&#x27;s correct. The <i>compilers</i> are wrong. Likewise newer better languages are unsuitable because the terse, meaningless programs won&#x27;t compile in such languages. New languages often demand specificity, the real programmer is obliged to spell out what they meant, which introduces the possibility that they&#x27;re capable of mistakes because what they meant was plain wrong.
评论 #28785078 未加载
bombcarover 3 years ago
The fact that a compiler is allowed&#x2F;encouraged to silently remove whole sections of code because of some obscure factoid is an amazing source of footguns.<p>At least the warnings are getting a bit better for some of these.
评论 #28779961 未加载
评论 #28779989 未加载
评论 #28780036 未加载
评论 #28779411 未加载
astrangeover 3 years ago
This is a typical whining about UB article, but removing it won&#x27;t get what you want, in particular your program still won&#x27;t behave correctly across architectures. Overflow on shift left may be undefined, but how do you want to define it? If you want a &quot;high level assembler&quot;, well, the underlying instructions behave differently on ARM, x86 scalar, and x86 SIMD.<p>The reason they claim program optimizations aren&#x27;t important is because you can do it by hand for a specific architecture pretty easily, but you&#x27;ll still want them when porting to a new one, eg if it wants loop counters to go in the opposite direction.
评论 #28780338 未加载
评论 #28784333 未加载
pdwover 3 years ago
How much of this is driven by modern C++ style? I always assumed optimizers needed to become much more aggressive because template-heavy code results in convoluted IR with tons of unreachable code. And UB-based reasoning is the most effective tool to prove unreachability.
评论 #28779911 未加载
评论 #28779815 未加载
gjm11over 3 years ago
The actual title of the paper is &quot;How ISO C became unusable for operating systems development&quot;. Is there a particular reason why the first and last words have been removed here?
评论 #28779449 未加载
davidgerardover 3 years ago
direct PDF link: <a href="https:&#x2F;&#x2F;www.yodaiken.com&#x2F;wp-content&#x2F;uploads&#x2F;2021&#x2F;10&#x2F;yodaiken_plos_c2-.pdf" rel="nofollow">https:&#x2F;&#x2F;www.yodaiken.com&#x2F;wp-content&#x2F;uploads&#x2F;2021&#x2F;10&#x2F;yodaiken...</a>
评论 #28779964 未加载
alerighiover 3 years ago
To me the stupid thing is the abuse of undefined behavior for changing the semantic of the code. The fact that a behavior is not defined in the standard doesn&#x27;t mean that on a particular hardware platform it doesn&#x27;t have a particular meaning (and most C programs doesn&#x27;t need to be portable, since C it&#x27;s mainly used for embedded these days and thus you are targeting a particular microcontroller&#x2F;SOC).<p>These optimizations leave for C++ folks. C doesn&#x27;t need all of that, just leave it as the &quot;high level assembler&quot; that it was in the old days, where if I write an instruction I can picture the assembler output in my mind.<p>Optimizers should not change the code semantics to me. Unfortunately with gcc it&#x27;s impossible to rely on optimizations, so the only safe option is to turn them off entirely (-O0).
评论 #28780132 未加载
评论 #28781177 未加载
评论 #28780144 未加载
po1ntover 3 years ago
I think it&#x27;s about time to abandon C language entirely.
评论 #28779979 未加载
评论 #28788301 未加载
评论 #28779427 未加载