2 ms·
> C has undefined behaviour where there is no universally sensible implementation across all architectures. But that's really the same thing. Java takes the
by MaxBarraclough 8y ago
> C has undefined behaviour where there is no universally sensible implementation across all architectures.
But that's really the same thing.
Java takes the opposite approach: it lays down the law, and if your CPU does things differently, the JIT compiler will have to generate however many extra instructions are necessary to bend things to Java's approach.
For instance, Java mandates 2's complement, where C does not. Java mandates the size of an 'int', where C does not. Java specifies exactly what happens if you attempt to dereference null, where C does not.
(Too late I see that oddity just made the same points.)
> In the first place it should make code faster
Well of course, or it's not optimisation, it's obfuscation.
> the general reception seems to be that exploiting undefined behaviour (by assuming it will never happen) rarely brings obvious speed advantages
Depends what you mean by 'obvious' I suppose. For instance, Java pays a non-zero performance penalty for insisting on range-checks for array-element accesses.
> I'll boldly state that optimizing out a "if (p == NULL)" only because p was dereferenced earlier never makes sense.
No need for boldness: Apple says that UBSan's performance penalty is around 20%. https://developer.apple.com/documentation/code_diagnostics/undefined_behavior_sanitizer https://developer.apple.com/documentation/code_diagnostics/u...
> Code containing such a check is never performance sensitive enough to justify the tradeoff
C programmers tend to hold some disdain for the attitude that they should be made to trade off performance for safety, and not without reason. Claiming they should 'never' care about the overhead of some check, isn't the C way.
If you want a safe language, don't use C. (Well, there's MISRA C, I suppose.)
> it should be easy to change compilers to NOT do this optimization
I don't know exactly what change you're proposing. If you manually check for null, the compiler can't just eliminate your check. Or do you mean C should always check for null at every dereference? The overhead there would be considerable.
> Performance is only a side effect
No, performance is central to the appeal of C.
- nerdponx 8y agoI don't know exactly what change you're proposing. If you manually check for null, the compiler can't just eliminate your check. Their post was predicated on the assumption that the compiler can in fact do this if 'p' was dereferenced before the check. Is that not the case?
- MaxBarraclough 8y ago> if 'p' was dereferenced before the check Oops, I misread! Yes, you've got undefined behaviour (i.e. a serious bug in your code), and as far as the compiler is concerned, anything goes. As the old joke goes, the philosophy of the C language is that the programmer is always right, even when they are wrong. i.e. that it's your job to ensure there's no UB in your source code. The 'C way' is not to have the language shield you from your mistakes. You can't have that without losing C's advantages. Fortunately, tools exist to help detect undefined behaviour. It's a pity that runtime detection of easily-detected UB hasn't been an option in C compilers since the beginning. For dev/debug builds, that's almost certainly what you want.
- fao_ 8y ago> i.e. that it's your job to ensure there's no UB in your source code. Have you seen the UB list though? It seems to me that almost all programs fall under some kind of UB. It would be nice to have the compiler produce warnings where it would otherwise optimize based on that UB.
- MaxBarraclough 8y agoUnfortunately it's a theoretical impossibility to detect all instances of UB in a static-analysis. Rice's theorem strikes again. These days we have pretty good static analysis tools, and tools like UBSan which can insert certain checks at runtime. Not perfect though. I believe it's possible to approach it from the other side though: use MISRA C, a terribly strict C programming ruleset, which I believe can be mechanically verified, and which avoids any possible UB. (This is an 'overly strict' approach, as it forbids doing certain perfectly legal things.)
- AstralStorm 8y ago1. No they don't. 2. The easiest ones to hit by accident are integer overflow ones followed by trapping floating point math. 3. Pointer aliasing is hard to hit if you're not baiting it. 4. Threading requires Data Race Free code. No language known validates this enough. 5. Unchecked pointer dereference and lifetime problems can be avoided especially in C++. It is a subset of missing input validation which again is not enforced in most programming languages. 6. Object slicing rarely comes into play on its own. 7. Format string mess also easy to avoid, s are weirdness caused by longjmp, signals and other ways of messing with the stack. There are 193 UB cases in C99 and most of them are redundant and group able. Bunch of them can be called "programmer brain is out to lunch". The list is bigger slightly in C20.
- pjmlp 8y ago> No, performance is central to the appeal of C. It surely didn't felt like that using 8 and 16 bit compilers up to early 90's.
- MaxBarraclough 8y agoSure, early C compilers would generate inefficient code. So what?
- pjmlp 8y agoIt contradicts your statement why people were using C in first place. We were using it because we were porting code from and to UNIX systems, nothing else.
- MaxBarraclough 8y agoYou're right I was too dismissive of the non-technical reasons that a language like C can do well: existence of legacy code, and the fact that all serious programmers already know C. At the time though, all compiler-generated code would have been at least as bad, no? With the possible exception of FORTRAN. Performance is relative. Assembly is rarely used today even for high-performance code, as modern optimising compilers are so good. Today, we know it's not impossible for a new language to gain traction, but all new languages that are used in anger are much more high-level than C. With the single exception of the Zig language, no other language emphasises raw performance and low-level access the way C does. Rust isn't too far off I suppose (from what little I know about it). Incidentally, Zig has UB too, but it's designed to be nowhere near as troublesome as it is in C.
- pjmlp 8y agoAssembly was certainly a requirement on 8 and 16 bit micro computers, where high level languages had the role of modern managed languages. Abrash and Norton MS-DOS books expose this reality quite well. On 70's mainframes outside AT&T walls not so much. NEWP uses CPU instrisics. Burroughs architecture doesn't expose Assembly. Xerox PARC moved away from BCPL into Mesa for their Star and Pilot workstations. With the last generation using Mesa/Cedar. Speaking of which, BCPL was originally only meant for bootstrapping CPL, before getting a life of its own.