9 ms·
This seems like a crazy conversation to be having in 2016. We're seriously considering what semantics to attach for illegal operations? How about doing what eve
by twic 11y ago
This seems like a crazy conversation to be having in 2016. We're seriously considering what semantics to attach for illegal operations? How about doing what every other language of the last couple of decades does and just not allow them?
> I doubt anyone is willing to pay 2x performance for their C code to be more friendly. If they were, why are they writing in C?
Beats me why anyone is writing in C full stop.
- foxhill 11y agothey are not necessarily illegal operations; embedded systems and kernel development would be two such examples - an area where C is frequently used. i can list many reasons why i continue to use C, please, there's no need to suggest or imply that C has outlived its usefulness - it clearly hasn't.
- to3m 11y agoYes. The standard leaves a lot of things undefined precisely so that they can be legal, or do something meaningful - or, indeed, not - without causing the implementation to become non-standard because of it. This is what "undefined" means: what it say! It doesn't mean "illegal", and it doesn't mean "bad".
- scott_s 11y agoI think you are confused "implementation defined" and "undefined". If something is "implementation defined", it is legal for a C program to do it, even though the standard does not define what exactly must happen. Compiler writers are allowed to define a reasonable behavior for their situation. However, "undefined" does mean "illegal". Not only does the standard not say what will happen, the standard says that such behavior is not allowed in well-formed C programs. C compilers, then, tend to optimize based on the assumption that certain behaviors will not happen. This can causes weirdness when they encounter programs that exhibit this behavior - and yes, such programs have a bug. But often, the bug manifests in an even stranger way. Check out the work of John Regehr for more (academic publications: http://www.cs.utah.edu/~regehr/papers/; http://www.cs.utah.edu/~regehr/papers/; blog: http://blog.regehr.org/ http://blog.regehr.org/).
- to3m 11y agoWell-formedness doesn't appear to have a definition in C11. In the C++11 standard, the wording rather implies to me that it is a static property, suggesting that a well-formed program could therefore still induce undefined behaviour at runtime. The C11 standard is however quite clear on what undefined behaviour is: "behaviour...for which this International Standard imposes no requirements". That sounds like a far cry from illegality to me! It sounds more like it is merely... yes... undefined. This means that gcc (et al) are allowed to do what they do. It doesn't mean it's not crap.
- scott_s 11y agoI think you omitted the "erroneous" part of that definition. From http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf: 3.4.1 1 implementation-defined behavior unspecified behavior where each implementation documents how the choice is made 2 EXAMPLE An example of implementation-defined behavior is the propagation of the high-order bit when a signed integer is shifted right. 3.4.3 1 undefined behavior behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements 2 NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). 3 EXAMPLE An example of undefined behavior is the behavior on integer overflow. And later, 4. Conformance 2 If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint or runtime constraint is violated, the behavior is undefined. Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior. There is no difference in emphasis among these three; they all describe ‘‘behavior that is undefined’’. ... 5 A *strictly conforming* program shall use only those features of the language and library specified in this International Standard.3) It shall not produce output dependent on any unspecified, undefined, or implementation-defined behavior, and shall not exceed any minimum implementation limit. Substitute "strictly conforming" where I said (colloquially) "well-formed". Footnote 3 basically says that a program can still be strictly conforming if it uses of conditional features has conditional guards. All of that above tells me: "implementation-defined" and "undefined" are different concepts in the C standard, and programs with "undefined" behavior are erroneous - or, colloquially, illegal programs. Oddly, I still agree with your final conclusion: gcc and other compilers are definitely allowed to do what they do; they are following the standard.
- yarrel 11y agoIt's a fast, simple, low-level language that despite the best efforts of standards bodies is easy to reason about.
- nickpsecurity 11y agoIt's not easy to reason about. The static analysis and formal verification results that were straight-forward to obtain in languages like ML and Modula took many Master's and Ph.D.'s worth of work for C. Human review also proved to catch less than safer, systems languages in many case studies because it just has too many gotchas. In common cases, too, rather than obscure ones. So, no, C is not easy to reason about. Nor are the tools that can cheap if you want low, false positives.
- swiley 11y ago>Beats me why anyone is writing in C full stop. Deterministic execution, type checking at compile time, portability are all just three reasons.
- bdamm 11y agoThat's silly. C is legendary for non-deterministic execution. Macros make a mess of type checking. And portability is possible but only with a tremendous amount of work for any library or product large enough to matter, with many different compilers and platforms bringing their own flavor of library semantics - if they're even available. No, people write C because it's the only choice in many cases. Like where you need total control over memory management, or because you want to interact with a kernel or hardware directly, or because you want to write functions in the lingua franca of compiled code.
- Coding_Cat 11y agoI often write small bits in C because it's simply fun. And I tend to get bogged down over-abstracting when I'm working in something more modern. The simplicity of C is a boon, allowing me to focus on one small problem and easilly study the performance. (I have spend the last 2 days making a thread-pool and BLAS system in C for fun, initially results are quite good. My 4x4 matrix-matrix multiplication algorithm beats Intel's own example :D). I wouldn't be quick to use it for a large scale project which does not require 110% performance but for anything were performance is your nr. 1 goal I've found it quite refereshing.
- exDM69 11y ago> My 4x4 matrix-matrix multiplication algorithm beats Intel's own example You know you can't make such statements without putting your money where your mouth is. We want to see the code. Here's three different 4x4 matrix multiplication routines I've written (the mmmul functions). Depends on your cpu which is fastest. https://github.com/rikusalminen/threedee-simd/blob/master/include/threedee/matrix.h https://github.com/rikusalminen/threedee-simd/blob/master/in...
- pcwalton 11y ago> How about doing what every other language of the last couple of decades does and just not allow them? I fully agree. But this is not without tradeoffs: eliminating undefined behavior pretty much means (a) the performance and runtime overhead of a GC (most languages); (b) forbidding malloc (verified "mission-critical" variants of Ada, C, etc.); (c) requiring programmers to learn a lot of new concepts (Rust†). I do think that there is rapidly becoming little reason to use C except for throwaway programs, and as an industry we need to be more open to (c) if we are ever going to move beyond making the same memory management mistakes we've been continuously making since the 1960s. But I also understand the reasons why programmers continue to choose C and C++. † It's interesting to me that the biggest reason C++ programmers give for bouncing off Rust is fundamentally "I want my undefined behavior back", though very few actually word it like that.
- nickpsecurity 11y agoNo it doesn't: just means we know what it will do. It might still fail. It might still use a risky approach that allows high optimization. One can run old Pascal or Modula-2 with unsafe constructs to do anything C can do at high performance. Disallowing undefined behavior simply means the result is defined behavior. Might be safe, unsafe, any number of things. Just clearly that to developer and tools.
- pcwalton 11y agoThe point of the article is that precisely what you suggest will have extremely bad consequences for optimization. In your proposal, how do you propose to solve the load-load forwarding issue described in the article?
- jryan49 11y agoIt's a high performance, portable, systems programming, with a huge history. That's why people still use it. I'd argue most programmers today don't need to program in such a language.
- nickpsecurity 11y agoAlso known as the legacy effect. Like COBOL, the cost to rewrite, retrain, retool and so on for new language is too high to be worth it. There's also a certain mindset the new thing would have to be compatible with that's somewhat unique to C. Even C-like languages such as Cyclone couldn't break past that.
- RodgerTheGreat 11y agoI like the term "platform nepotism" to describe the fact that in Unix, C has special treatment from the perspective of APIs and ABIs. If you want to interact with the kernel or standard system libraries you must use a C-style calling convention against routines designed with the C type system in mind. Any other language must pay the cost of closing that gap, whether the cost is paid in extra tooling, clumsy rearrangement of activation records, typecasts, loss of type safety, using a restricted subset of normal language features, etc. A vast selection of libraries for most platforms impose similar bottlenecks. Unless you're willing to uproot an immense pre-existing platform, C will always have an upper hand. Reinventing all those wheels will take a long time, so it's good to see that there are a few efforts already underway.
- nickpsecurity 11y agoVery, very well-said. There is no out when one is locked into broken architecture. Any escape comes at a cost.
- jryan49 11y agoI wouldn't call it "broken". The tools we have today will be "broken" by tomorrow's standards as well.
- overgard 11y ago> This seems like a crazy conversation to be having in 2016. We're seriously considering what semantics to attach for illegal operations? How about doing what every other language of the last couple of decades does and just not allow them? If every important program on your computer (The ones written in C -- OS, web browser, text editor, language interpreters, etc.) suddenly became twice as slow for "safety", assuming you never modify those programs on your own, you'd probably be a little bit cross right? You wouldn't really care that the language is safer, you'd just notice your top of the line computer is suddenly feeling pretty slow.
- pcwalton 11y ago> If every important program on your computer (The ones written in C -- OS, web browser, text editor, language interpreters, etc.) suddenly became twice as slow for "safety", assuming you never modify those programs on your own, you'd probably be a little bit cross right? Exactly. Read the comments on literally any thread about browsers on HN to understand why sacrificing that degree of performance is unacceptable.
- jotux 11y ago>Beats me why anyone is writing in C full stop. Embedded software? All of the platforms I work on provide C/C++ compilers and driver libraries that are C only. I'm working on an ARM Cortex project right now and there isn't even C++11 support.