11 ms·
My complaint against C is that some of the smartest people on the planet use it to write infrastructure that regularly fails catastrophically. If the experts wh
by kstrauser 10y ago
My complaint against C is that some of the smartest people on the planet use it to write infrastructure that regularly fails catastrophically. If the experts who love it aren't capable of writing correct code, I have no chance whatsoever. Humans just aren't good at the fiddly bits (remembering to free(); not to free() twice; not to use after free(); accidentally adding a second clause to a non-bracketed if expression; how many times to we see these in vulnerability reports?).
C is obviously very powerful. There's no denying that. But it does approximately zero of the things to help programmers that a modern language does. It's not even strongly typed in the usual sense (is there the equivalent of casting void pointers anywhere else?).
Finally, C is slow. Yeah, you read that right. Its main problem is that it has no non-wizardry high level semantics for parallelism. For example, modern languages have map() to say "do this operation on all of these items". In C, you have to write that as "for each item in this collection, do this thing, then the next, then the next, ...". If the compiler is exceptionally smart, it might infer what's going on and help out. But with map(), you're explicitly telling the language "I want all of these things to be done" and it doesn't have to infer anything.
I respect C. It's given us a lot of great stuff. But there's no way I'd willingly start a new project in C today given the alternatives. I am dumb, and given the choice between languages which help me and languages which are basically a small step up from assembler, I'm going with one that gives me a reasonable shot at writing safe, fast, and understandable code.
- dzdt 10y agoAre there any real world examples (with actual measurement) of languages where map uses hardware parallelisation to be faster than a c loop? Serious question! My impression is, no, outside of something like hadoop.
- lallysingh 10y agohttps://donsbot.wordpress.com/2007/11/29/use-those-extra-cores-and-beat-c-today-parallel-haskell-redux/ https://donsbot.wordpress.com/2007/11/29/use-those-extra-cor... This was a simple fib(), not a map(), but I think the point still stands pretty well.
- kutkloon7 10y agoFPGA implementations usually are made to do this, although there is usually a human writing dedicates VHDL. Also google course-grained reconfigurable architectures (which can sometimes be mapped automattically from code).
- deleted 10y ago[deleted]
- kstrauser 10y agoI don't have any off the top of my head, but I used a forking map in Python where the inner call was an image processing operation, and it was approximately #cores faster. I think it'd be enormously dependent on workload: if the inner operation is adding two ints, the gains might be lost in the parallelization overheard. Map is just one example of a high-level construct that permits faster code, though. A lot of us here have done async IO in C because that was the option we had available to us for doing it, but I'd vastly rather do such stuff in Python (3), Go, or Node where the language itself gives you constructs for describing what you're trying to accomplish. My impression is, no, outside of something like hadoop. That's a pretty big exclusion, though! We use Hadoop-like stuff on, ahem, sizable quantities of data. That's not exactly an edge case.
- Ono-Sendai 10y agoHere's an example (something of a proof-of-concept/toy example though) Auto-vectorisation and parallelisation in Winter: http://www.forwardscattering.org/post/22 http://www.forwardscattering.org/post/22
- pjmlp 10y agoCheck the now dead Connection Machine and *Lisp.
- taeric 10y agoMy concern is this seems like it's own fallacy now. You are assuming the same experts could have written these systems in another language. Could they? Maybe. But there is no solid evidence that they could. Is the argument appealing? Yes. But it is lacking the evidence of these systems that could be better in other languages. My 2 cents for why, is that c is basically the only game in town that can easily use new and special assembly. Of which there is a lot. Consider, what is the fastest way to calculate a square root? And how old is that instruction? (I'm assuming it is actually used by higher level languages by now.)
- kstrauser 10y agoI don't think that's true. Let's pick the Node.js ecosystem for an example (chosen because I use it very little, so I'm not hyping up my own pet language). Lots of non-expert programmers are writing Node packages, both for their own in-house use and to share with others. I'm going to assert that the average Node user has fewer years programming than the average C user. And yet, they seem to have fewer critical vulnerabilities per amount of code written. They still make mistakes all the time, sure! But they're a higher class of mistakes, more "this sets the wrong HTTP status code" and less "this exposes kernel memory to remote users". JavaScript handles the low-level abstractions so that its users can concentrate on breaking the higher-level stuff, hopefully in ways that are easier to test and notice. Also, almost every language has a way to use C code directly (For example, see Go: https://golang.org/cmd/cgo/ https://golang.org/cmd/cgo/). C may be the best way to write low-level driver code. If it is, use it for that! I'm not convinced that there's a single best language for Ethernet card drivers and HTTP stacks, though.
- taeric 10y agoWhat systems are we talking about, then? Few people are writing stuff in C/C++ nowdays. And the fields that do this on a regular basis are pretty much unchallenged by any of the higher level languages. And, agreed, on truly expert systems, things do go polyglot. This has been true since pretty much the beginning of computers. I'm not sure what sort of evidence this provides, though. So, to bring it directly to point. Where is the evidence that the systems people typically write in C could be written well in another language? About the best I have seen is some system utilities written in rust. Which is awesome, no doubt. And the beginnings of the evidence I'm asking for. (To be fair, we often speak of mysterious wizards that wrote the symbolic lisp systems as though they succeeded at this. I want that to count as evidence, but it feels more like hearsay.)
- simondedalus 10y agoTo be fair, the value of the features you cite are very dependent on the size of the team and the type of work being done. If you're throwing tons of engineers, tests, and money at something--think aircraft or whatever--you're not relying on the language to be smarter than the programmer. And as for operating systems being written in C and failing, writing a general purpose, hardware agnostic operating system isn't exactly easy. This is not to say that C should be the systems language of choice, or even that C is easier or more stable to write operating systems; just that stuff will be buggy regardless. And I think your main complaint is stuff like OpenSSL and its vulnerabilities... I don't think I would blame the language there. I would blame age / inherited bugs / lack of testing (amazingly that's the case, but we don't live in a world that's too concerned with the common good). I agree with you for many, many use cases for a programmer these days... but only if you just contextualize all these assertions.
- nickpsecurity 10y ago"And as for operating systems being written in C and failing, writing a general purpose, hardware agnostic operating system isn't exactly easy." Wirth and Jurg did it in a year or two on custom hardware using Modula-2. They and thdir students repeatedly did it with Oberon variants. They were all safer and more reliable than earliest UNIX distro. Partly due go strong typing, interface checks, and GC's in later versions. Similarly OS's written with Modula-3 (Spin), Haskell (House), Java (JX), and Rust (Redox) moved fast since the languages reduced number of problems they had. Spin could even dynamically link M3 code into running kernel with type system preventing many crashes or vulnerabilities. Made for great acceleration of networking apps, etc.
- simondedalus 10y agoI know that the response I'm about to give makes a statement that is not falsifiable and very much has the form of "I can cart this out and think I'm right no matter WHAT you respond," but I still feel compelled to say... ...the point I'm making is absolutely not that building an OS is equally hard in all languages, and equally likely to be buggy. My point is that creating a general purpose OS that only gets its rigorous testing from getting put into production before the majority of its target hardware has even yet been developed... that's going to be buggy in all languages. Designing and running an OS on a known system with a few applications in mind can't really refute what I'm saying. I mean, I could of course be wrong. But I can't imagine a convincing counter-example that doesn't have for its evidence "largescale adoption on disparate systems." (there could be an in-principle reason that I'm wrong that I just don't understand, but this is the risk we take in having opinions)
- bluecalm 10y ago>>remembering to free(); not to free() twice; You can set the pointer to NULL, then freeing again won't hurt >>not to use after free() Set the pointer to NULL so it will crash. >> Its main problem is that it has no non-wizardry high level semantics for parallelism. Fair, the language doesn't have it. For everyday programming though you can use something like OpenMP where making things execute in parallel often requires adding one line above your for loop. >>But there's no way I'd willingly start a new project in C today given the alternatives There is no alternative for performance critical code (well, at least among popular languages of today). For other things - sure. >>I'm going with one that gives me a reasonable shot at writing safe, fast, and understandable code. If safety is your priority then likely C is not for you. As to performance it depends what you consider fast. If making stuff 50% faster no matter how fast it already is saves a lot of money you have no choice but C/C++/Fortran or just hand code in assembly.
- steveklabnik 10y ago> Set the pointer to NULL so it will crash. Dereferencing NULL is not guaranteed to crash. It's undefined behavior, generally. SO that can lead to tons of other problems.... > There is no alternative for performance critical code On many platforms, Rust exists today. C is still the king of that, though, especially for obscure embedded stuff.
- bluecalm 10y agoRight. Still, in practice, if you are setting pointers to NULL after freeing them (to say "there is nothing there" if some other part of the program wants to check) you will get a crash (as the compiler won't be able to prove the pointer is NULL if setting it to NULL depends on what happens at runtime). I agree it's not perfect solution, just something to help you catching errors in your logic (not checking for NULL when something could be NULL is a bug in program logic).
- steveklabnik 10y agoIf the zero address is valid, then one aspect of "undefined behavior" is that the result is "this will actually continue to 'work' until something random goes wrong". Someone just the other day on Reddit was talking about how this is a common issue in their workplace, as they work on a system that maps valid memory to 0.
- Kenji 10y agonot to use after free(); accidentally adding a second clause to a non-bracketed if expression; Two methods I religiously use in my code: Set to NULL after freeing. free(NULL) is a no op, so freeing twice is never an issue anymore. And since you probably won't use the variable after that anyway, setting it to NULL eventually gets optimized out anyway, probably. And no if without brackets in my codebase.
- StillBored 10y agoFinally, C is slow. Yeah, you read that right. Its main problem is that it has no non-wizardry high level semantics for parallelism. Do you know about openMP? Which works in most mainstream c compilers (clang, gcc, icc, msvs, etc). Ok, its not part of the standard, but IMHO thats probably a good thing. C doesn't dictate a paradigm which allows it to support a wide range of competing standards. Pick the one best for your project, or embed something like openCL or lua right into your C project. Of course the opposite can be said as well, write your code in python/whatever and then write a C plugin for the critical portions. Because, outside of fortran or C++ pretty much nothing comes close to the capability of C to solve a problem quickly. Which if no one noticed is even more important these days than it was two decades ago, when companies used crappy languages and shipped products that wasted 99% of the cycles of the users computer. Today doubling the performance of your product can literally 1/2 the computing costs at your friendly cloud provider/datacenter.
- nickpsecurity 10y agoExactly. I studied HPC over a decade ago. All C and Fortran dialects with amazing performance. Today, they advertise updated versions of same languages.
- ArkyBeagle 10y ago"...some of the smartest people on the planet use it to write infrastructure that regularly fails catastrophically." Not one thing said in that sentence fragment can be shown categorically to be true. Yo, software has defects. Some get past everybody. To wit: 1) It's 2016. There's a lot of old code out there. Old code tends to be C. 2) UB and implementation-defined behavior are just a fact in the language. This being said, they're not THAT hard to avoid. I think the thing is whether or not people are habituated to thinking in constraints. People who aren't will be more dangerous with a C compiler than those who are. FWIW, I've heard the phrase "knuckledragger C programmer" more than once. As a C programmer, it might even be apt. :)As in "monads? We don't need no steenking monads!" :) ( this being said, I use something akin to a monad pattern quite frequently and in C )
- smitherfield 10y agoIf you tilt your head and look at it funny, p == NULL p *p is a monad. :)