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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Re: C as used/implemented in practice

114 点作者 davidtgoldblatt将近 10 年前

10 条评论

nathanb将近 10 年前
I have difficulty accepting &quot;let&#x27;s replace C with X&quot;, where X is a memory-managed language. As a systems programmer (I write SCSI driver code in C), I can&#x27;t overemphasize how important it is to be able to address memory as a flat range of bytes, regardless of how that memory was originally handed to me. I need to have uint8_t* pointers into the middle of buffers which I can then typecast into byte-aligned structs. If your memory manager would not allow this or would move this memory around, that&#x27;s a non-starter.<p>I don&#x27;t stick with C because I love it. If I&#x27;m writing something for my own purposes, I use Ruby. I&#x27;ve written some server code in Golang (non-production), and it&#x27;s pretty nifty, even if the way it twists normal C syntax breaks my brain. I even dabble in the dark side (C++) personally and professionally from time to time. And in a previous life, I was reasonably proficient in C# (that&#x27;s the CLR 2.0 timeframe; I&#x27;m completely useless at it in these crazy days of LINQ and the really nifty CLR 4 features...and there&#x27;s probably even more stuff I haven&#x27;t even become aware of).<p>But none of those languages would let me do what I need to do: zero-copy writes from the network driver through to the RAID backend. And even if they did, the pain of rewriting our entire operating system in Go or Rust or whatever would be way more than the alleviated pain of using a &quot;nicer&quot; language.<p>(We never use &#x27;int&#x27;, by the way. We use the C99 well-defined types in stdint.h. Could this value go greater than a uint32_t can represent? Make it a uint64_t. Does it need to be signed? No? Make sure it&#x27;s unsigned. A lot of what he&#x27;s complaining about is sloppy code. I don&#x27;t care if your compiler isn&#x27;t efficient when compiling sloppy code.)
评论 #9881968 未加载
评论 #9882040 未加载
评论 #9883190 未加载
评论 #9882203 未加载
评论 #9881985 未加载
评论 #9882370 未加载
评论 #9883683 未加载
评论 #9883653 未加载
评论 #9882034 未加载
评论 #9882278 未加载
byuu将近 10 年前
I understand that in some cases, these heroic compiler optimizations can offer significant performance increases. We should keep C around as it is for when said performance is critical.<p>But surely, we can design a language that has no undefined behavior, without substantial deviations from C&#x27;s syntax, and without massive performance penalties. This language would be great for things that prize security over performance.<p>And the trick is, we don&#x27;t need to rewrite all software in existence in a new language to get here! C can be this language, all we need is a special compilation flag that replaces undefined behavior with defined behavior. Functions called inside a function&#x27;s arguments? Say they evaluate left-to-right. Shift right on signed types? Say it&#x27;s arithmetic. Size of a byte? Say it&#x27;s 8-bits. memset(0x00) on something going out of scope? If the developer said to do it, do it anyway. Underlying CPU doesn&#x27;t support this? Emulate it. If it can&#x27;t be emulated, then don&#x27;t use code that requires the safe flag on said architecture. Yeah, screw the PDP-11. And yeah, it&#x27;ll be slower in some cases. Yes, even <i>twice</i> as slow in some cases. But still far better than moving to a bytecode or VM language.<p>And when we have guaranteed behavior of C, we can write new DSLs that transcode to C, without carrying along all of C&#x27;s undefined behavior with it.<p>You want to talk about writing in higher-level languages like Python and then having C for the underlying performance critical portions? Why not defined-behavior C for the security-critical and cold portions of code, and undefined-behavior C for the critical portions?<p>Maybe Google wouldn&#x27;t accept the speed penalty; but I&#x27;d happily drop my personal VPS from ~8000 maximum simultaneous users to ~5000 if it greatly decreased the odds of being vulnerable to the next Heartbleed. But I&#x27;m not willing to completely abandon all C code, and drop down to ~200 simultaneous users, to write it in Ruby.
评论 #9883216 未加载
jeffreyrogers将近 10 年前
For those who don&#x27;t know Chris Lattner[1], who wrote this post, is the primary author of LLVM and more recently of Swift, so he knows a bit about what he&#x27;s talking about :)<p>[1]: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Chris_Lattner" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Chris_Lattner</a>
carlosrg将近 10 年前
Until I see really big and open source projects like WebKit or Clang itself moving to Swift or whatever, anything I read about moving to &quot;better systems languages&quot; is like reading a letter to Santa Claus. I doubt C++ is going anywhere, especially when C++ itself is not standing still and evolving (C++11, 14, 17...) while maintaining backwards compatibility.
评论 #9884068 未加载
pjmlp将近 10 年前
&quot;My hope is that the industry will eventually move to better systems programming languages, but that will take a very very long time...&quot;<p>-- Chris Lattner<p>Yes, a very long time. Modula-2 was born in 1978, but we can go back to Algol and Lisp even.
mcguire将近 10 年前
&quot;<i>In the first example above, it is that &#x27;int&#x27; is the default type people generally reach for, not &#x27;long&#x27;, and that array indexing is expressed with integers instead of iterators. This isn’t something that we’re going to &#x27;fix&#x27; in the C language, the C community, or the body of existing C code.</i>&quot;<p>The majority of that message is pretty well said, but this particular part leaves me cold. The problem <i>isn&#x27;t</i> that &#x27;int&#x27; is the default type, not &#x27;long&#x27;, nor is it that array indexing isn&#x27;t done with iterators. (Ever merged two arrays? It&#x27;s pretty clear using int indexes or pointers, but iterators can get verbose. C++ does a very good job, though, by making iterators look like pointers.) The problem is that, in C, the primitive types don&#x27;t specifically describe their sizes. If you want a 32-bit variable, you should be able to ask for an unsigned or signed 32-bit variable. If you want whatever is best on this machine, you should be able to ask for whatever is word-sized. Unfortunately, C went with char &lt;= short &lt;= int &lt;= long (, longlong, etc.); in an ideal world, &#x27;int&#x27; would be the machine&#x27;s word size, but when all the world&#x27;s a VAX, &#x27;int&#x27; means 32-bits.<p>That is one of the major victories with Rust: most primitive types are sized, with an additional word-sized type.
评论 #9883335 未加载
mrpippy将近 10 年前
For the for loop example, is there some reason why clang doesn&#x27;t output a warning like &quot;Does &#x27;i&#x27; really need to be signed? If so, explicitly make it a &#x27;signed int&#x27;. Otherwise, change it to be unsigned&quot;
评论 #9882118 未加载
评论 #9882398 未加载
nikanj将近 10 年前
Once again, <a href="http:&#x2F;&#x2F;research.microsoft.com&#x2F;en-us&#x2F;people&#x2F;mickens&#x2F;thenightwatch.pdf" rel="nofollow">http:&#x2F;&#x2F;research.microsoft.com&#x2F;en-us&#x2F;people&#x2F;mickens&#x2F;thenightw...</a>
ryanmarsh将近 10 年前
Have we lost sight of the fact that when we talk about a programming language we&#x27;re really talking about how to put bits on CPU registers?
评论 #9889235 未加载
JustSomeNobody将近 10 年前
What would be considered &quot;security critical&quot;? SSH? IPTables? Linux kernel?
评论 #9883160 未加载