29 ms·
Author here. I'll be blunt and repeat a prediction I made 3 years ago or so: C is finished if it doesn't address the buffer overflow problem, and this proposal
by WalterBright 6y ago
Author here. I'll be blunt and repeat a prediction I made 3 years ago or so:
C is finished if it doesn't address the buffer overflow problem, and this proposal is a simple, easy, backwards compatible way to do it. It is simply too expensive to deal with buffer overflow bugs anymore.
This one addition will revolutionize C programming like adding function prototypes did.
- FpUser 6y agoPascal (Delphi and FreePascal incarnations) does that just fine with strings and dynamic arrays and in a way that is compatible with C. Just friggin' steal it an be done.
- baby 6y agoI don't see a future where C survives, not only because of memory corruption bugs (although that's a pretty big one), but also for usability: the lack of package manager, common build system, good documentation, good standard library, etc. are just too much to compete with any modern system language.
- forrestthewoods 6y ago> I don't see a future where C survives if C dies then what replaces it?
- nicoburns 6y agoPerhaps a combination of a language like Zig (a 1:1 replacement for situations where you really do want a lot of manual low-level control) and higher-level languages like Rust eating into more and more of the use cases.
- humanrebar 6y agoI don't see C being in much worse shape than C++ with respect to build system and package manager. It's slow going, but progress seems to be happening there. Are you saying both are doomed? Or is there some scenario where C++ survives without C?
- vlovich123 6y agoI think both are, long term (think FORTRAN where it’s not particularly popular but a lot of existing code is maintained and not rewritten). C++ is actually in a slightly better spot ironically because it’s harder to integrate with. If you have a C program you can pretty easily start replacing parts with Rust. You can’t do the same with C++ which insulates it better in that sense.
- ryl00 6y agoReports of Fortran's death (latest standard 2018) are greatly exaggerated (much like C). It's receded to a niche, but it's still a very important niche (numerical, HPC). Hopefully, the development of a new Fortran front end for LLVM (from PGI/Nvidia?) pans out, as this would fill a gap in LLVM's offerings, and provide more competition for ifort and gfortran.
- vlovich123 6y agoYou’re proving my point. FORTRAN is a niche language. C++ is still mainstream. It will recede but not completely disappear
- baby 6y agoI don’t see a great future for C++ either
- edoceo 6y agoThose are features which makes C flexible on main-stream platforms and also usable for so many other platform where other languages just don't/won't work.
- pjmlp 6y agoThose features are not unique to C, they are just cargo culted as such.
- pjmlp 6y agoUnfortunely until we get rid of UNIX/POSIX clones, C will be kept around. So not in my lifetime.
- mark-r 6y agoOr at least not in Torvalds' lifetime. His views on replacements for C are legendary. It would be interesting to see his comments on this proposal.
- ratww 6y agoTorvalds himself might have a different opinion: People have been looking at that for years now. I’m convinced it’s going to happen one day. It might not be Rust, but it’s going to happen that we will have different models for writing these kinds of things https://thenewstack.io/linus-torvalds-on-diversity-longevity-rust-and-arm-chips/ https://thenewstack.io/linus-torvalds-on-diversity-longevity...
- deleted 6y ago[deleted]
- fortran77 6y agoC will survive, if just for embedded/systems programming where you need a "portable assembly language" that can run on the simplest CPUs.
- mhh__ 6y agoThat's because of sunk-cost rather than design. Thanks to LLVM and GCC you can happily write embedded code in a higher level language, but the vendors don't bother supporting it because a lot of embedded coding isn't really what we would call software (no tests etc.)
- unsatchmo 6y agoThe higher level languages are kind of the problem though. I need things like the ability to know the layout of my structs.
- mhh__ 6y agoRust, D, Whatever let you control the layout of your struct.
- ComputerGuru 6y agoEven C#, actually!
- snovv_crash 6y agoToolchains are one side, but garbage collection and big standard libraries are also a big reason. Anything with under a MB of RAM has a choice of several modern languages, but it is still basically just C, C++, Rust, Lua or MicroPython.
- mhh__ 6y agoD works fine on microcontrollers. I don't think anyone was going to write their fridge's code in Haskell anyway.
- ajsnigrutin 6y ago> I don't see a future where C survives I've been seeing those exact words for decades now, and C is still going strong. Every few year a new language comes, somes writes something in it, that was written in C before, someone might even write a basic OS in it, and after a few years, that language is almost forgotten, a new one is here, and again, someone is writing something in it, but in the end, we still use C for the things we used it 10, 20, for some, even 30 years ago.
- diegocg 6y agoUsage of C in new projects has fallen dramatically in the latest decades. It used to be the case that C was considered a general purpose programming language and applications such as Evolution were written in it. Today big applications in C are increasingly rare, and Rust is only accelerating this trend - nobody wants to have buffer overflows anymore.
- Skunkleton 6y agoDo you have a citation? Genuinely curious.
- blogant 6y ago> lack of package manager, common build system, good documentation. This is where C is superior to virtually every other language. It has K&R to start with [1], a wealth of examples to progress from there, man pages, autotools, cmake, static and shared libraries. > good standard library. It should have hash tables at least, but it isn't bad. [1] Which is still the best language book ever written (yes, it has some anti patterns, you unlearn them quickly).
- johnisgood 6y ago> It should have hash tables at least https://man.openbsd.org/ohash_init.3 https://man.openbsd.org/ohash_init.3
- ComputerGuru 6y agoWhat of it? > Those functions are completely non-standard and should be avoided in portable programs.
- johnisgood 6y agoSure, it has not been standardized, it is not part of the standard library, so what? Did the world stop? I mean, practically speaking, who cares? Implement it, or find libraries that did. There are plenty. I posted this one because it exists for an OS; OpenBSD, since 1999. Plus, AFAIK ohash is portable enough. It consists of 2 files, and you can compile it with -std=c89. Only the bounded attribute is ignored. If you want I could have brought up hcreate, hdestroy, and hsearch: > The functions hcreate(), hsearch(), and hdestroy() are from SVr4, and are described in POSIX.1-2001 and POSIX.1-2008. Happier?
- ComputerGuru 6y agoI use stb myself, so I have no qualms with that. The point is rather that GP was discussing praise for C’s standard library, and even the most portable single-file include-only dependency remains just that: an external dependency that isn’t part of the C standard library (and no, posix isn’t C).
- non-entity 6y agoAs much as I dislike C > There are only two kinds of languages: the ones people complain about and the ones nobody uses. This unfortunately seems to mostly hold true.
- unsatchmo 6y agoPorque no los dos? There are a few languages that nobody uses and also everybody seems to complain about.
- non-entity 6y ago> mostly
- saagarjha 6y agoMUMPS.
- yesenadam 6y agoComplaining about language doesn't seem off topic here: "Porque" means Because. "Por que" means Why.
- dependenttypes 6y ago> the lack of package manager Just use nix or even apt. Both of them are MUCH better when compared to trash like npm or cargo which do not even check for signatures. > common build system Such as make? There is also Ninja/Meson if you prefer.
- samatman 6y agoSQLite alone has a support contract through 2050. C survives.
- rumanator 6y ago> I don't see a future where C survives Meanwhile C is running strong since the 70s. > the lack of package manager What do you call linux distro's package managers then? I mean, in distributions like Debian you can even download a package's source code with apt-get.
- ImprobableTruth 6y ago>What do you call linux distro's package managers then? If you want to count them as package managers, they're by far the worst ones of all the well known languages (with some notable exceptions e.g. guix's and nixos's). They're not portable between distributions or even different versions of the same distribution (!), since it's non-trivial to install older versions of libraries (or, hell, different versions of the same library at the same time). Not to mention that it's a very manual and tedious process in comparison to all the other language specific package manager. 'Dependency hell' is a problem virtually limited to distro package managers (and languages like C and C++ that depend on them). Getting older, unmaintained C programs to run on Linux is an incredibly frustrating experience and I think a perfect demonstration of how the current distro package manager approach is wholly insufficient.
- account42 6y ago> If you want to count them as package managers, they're by far the worst ones of all the well known languages (with some notable exceptions e.g. guix's and nixos's). The have the only feature I care about: cross-language dependency management. Unless you are suggesting to reimplement everything in each language and then make users install ten different XML parser, SSL implementations, etc. just because not-implemented-in-my-favorite-language syndrome.
- pjmlp 6y agoOnly on platforms where UNIX is the name of the game.
- knaq 6y agoI definitely like the "lack of package manager, common build system". For me, having those is a negative for a language like rust. You see, my OS already comes with those, and I expect to use them. I have the Debian package system: dpkg, apt, aptitude, and so on. It's a big mess when other software tries to steal that role. I have the traditional build systems and more: make, cmake, autoconf, scons, and so on. If I'm building a large project with multiple languages, I'm going to use one of those tools. If a language wants to fight me on that, I'm not interested in that language.
- rightbyte 6y agoThe proposal is just syntactic sugar for an size argument. It doean't add or solve anything really.
- WalterBright 6y agoIn my experience with language design, a little bit of syntactic sugar can have transformative results. C's function prototypes, syntactic sugar added circa 1990, were transformative for C programming.
- Koshkin 6y agoI wonder if a better idea (in principle) would be to have some kind of hardware implementation, sort of like a finer-grained memory segmentation.
- ksherlock 6y agox86 has a BOUND instruction which generates an exception if an index is out of bounds. It didn't make it into x64.
- saagarjha 6y agoThis is likely coming in ARM soon. (And I'm hopeful it's not even soon™, or "it came but nobody used it" as has happened on x86.)
- knaq 6y agoCHERI is an attempt. DARPA paid to have it for RISC-V and ARMv8. It was originally for MIPS. https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/ https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/ It is fundamentally like the 80286 segments, but with all sorts of usability troubles solved. The 80286 segments were impractical because there were a small number available and because the OS couldn't safely hand over direct control. Every little segment adjustment required calling the OS.
- pjmlp 6y agoGoing forward Android will require hardware metadata extensions on ARM devices, as of Android 11.
- enriquto 6y agoThe real troubles are undefined behavior and aliasing. Buffer overflows are just a well known gimmick of the language that is more or less controllable with some discipline. Aliasing is hell. You cannot even use a global variable safely!
- xxpor 6y agoIsn't that what asan/ubsan is for? Granted, it's not static analysis, but it should catch most aliasing related errors, no?
- Ar-Curunir 6y agoonly if your tests exercise that code path
- identity0 6y agoYou can't do compile time bounds checking, if that's what you're implying.
- fizixer 6y agoI'm fully in the camp of C plus powerful analysis tools, plus a high-level language (Python or Scheme).
- pjmlp 6y agoSince powerful analysis tools aren't defined by ISO what they are supposed to be, there are plenty of C compilers that will never get them.
- nsajko 6y agoNo. There has been some effort in that direction, somebody proposed a Clang "type sanitizer" patch, but it wasn't merged.
- saagarjha 6y agoMost, but not all. They are excellent tools but not perfect by any means.
- eps 6y agoI'm not sure whom this proposal is aimed at exactly. Any production-quality C code will already use a (pointer + count) combo when passing arrays to a function, which is something that will still be needed under your proposal because the vast majority of arrays is dynamically sized. So unless all arrays in C are given the fat pointer treatment, I don't really see how what you suggest would make much of a difference. That is, if fat pointers are made the first class language construct, then, yes, that can be useful... though I disagree if it's not done, it will cause a demise of C.
- mhh__ 6y agoAny production-quality C code? Any or some? I'm not sure if I've seen that in the wild.
- leetcrew 6y agopointer + size does not really fix anything, as you are relying on the programmer to correctly keep track of the size. I'm not even sure what alternative this improves upon. even more error-prone null value marking the end? praying the array will be big enough (looking at you, gets!)? unless you have a team of incredibly diligent coders, people are going to read past the end of bare arrays over and over again. one specific mistake I keep seeing is where people misinterpret the meaning of a variable named `size`. is it the number of elements or the size in bytes? who knows, but it's probably UB either way if you're wrong.
- true_religion 6y agoWould you just wrap the pointer and size in a strut, then only iterate the array via a library of functions that check the size first? I don't code c full time, but it's what I have always done when needing to use c via ffi to get a speed up in a dynamic language.
- leetcrew 6y agoyou could do that, if you were using a library that provided/understood that struct. not sure how common this is; I work with c++ much more than c. the problem with this approach is that you are still relying on the application programmer to provide the correct size at the beginning and not to mess it up by directly accessing the struct member later. private/public does not really exist in c, so it is a lot harder to enforce invariants within an object. the library could make the struct layout a private implementation detail (ie, not fully define the struct in the header provided to the client and take a pointer to the struct as arguments in the API) to at least discourage this. you could combine this approach with a my_array_struct_init function that returns a pointer to an empty array object. this is a common approach taken in c libraries (eg, libcurl) where the author really doesn't want you messing with their structs.
- Upvoter33 6y ago"C is finished if it doesn't address the buffer overflow problem" You should keep making this prediction ... one day you might be right! :)
- teej 6y agoC code is being replaced by Rust fast. The only limit is how quickly programmers can become good at Rust. It's already happening.
- FpUser 6y agoI used to write whole bunch of firmware for microcontrollers and frankly C was doing just fine for me in this area.
- johnisgood 6y agoOne of the niceties of C is that I can get anything done pretty damn quickly, without anything getting in my way. The syntax is extremely simple, too, vs. Rust. Rust is close to Perl when it comes to syntax; full of symbols. Too implicit for me. I want to look at the code, and I want to understand what the heck is going on, even if it is written by someone else. I usually do, with C. Rust? Not so much, and believe me, I tried. I would not like to call myself an idiot, either. :) A viable C replacement is Ada, although it is not for people who dislike the "code is documentation" bit.
- diegocg 6y agoYoung programmers seem to prefer learning Rust than C. Generational replacement will take care of making Rust prevalent, no matter what existing programmers think.
- johnisgood 6y agoI believe we will come back to C eventually. Kind of irrelevant but: I learnt C by writing mods for ioquake3 forks. :) Fun times. I was about 13 years old.
- bumblebritches5 6y agoWalter, I honestly just can't trust your judgement on the future of C or C++, because all I ever see is you pushing D anytime you comment. This seems like just another gimmick to push D tbh.
- gallier2 6y agoDid you see that the article is from 2009?
- WalterBright 6y agoYour choices are: 1. the judgement of people in the industry, who will always be biased 2. the judgement of people not in the industry, who don't know what they're talking about As for me, I still sell a C and C++ compiler https://www.digitalmars.com/shop.html https://www.digitalmars.com/shop.html
- AnimalMuppet 6y agoWould you care to define "finished" tightly enough that we can objectively evaluate whether or not it happens? And put a time frame on it?
- bsder 6y ago> C is finished if it doesn't address the buffer overflow problem, and this proposal is a simple, easy, backwards compatible way to do it. Is this really "simple, easy, backwards compatible"? I think Rust kind of counterexamples this. While Rust can throw around slices [] (effectively runtime length), throwing around [u8; 8] and [u8; 9] (compile time length) to the same function gets nasty. Perhaps all the constexpr work in Rust will make this a lot easier.
- ComputerGuru 6y agoNot constexpr, this is solved by constant generics (the famous RFC 2000) with the array bound as a constant “generic” parameter/value. I’ve been pushing for this feature for many years and have been playing with it since it first landed in nightly. It works quite well.
- bsder 6y agoSorry, I misspoke. But this wasn't meant to be about Rust. My point was that: if you dump slices/fat pointers into C, how does it help? Slices/fat pointers and the corresponding checks are a runtime thing, and that's absolutely anathema to a lot of C programmers. If it's not anathema to the C programmer, they probably aren't in C anyway. So, now you need a way so that compiled slices/fat pointers mean something, and I'm not convinced that doesn't have a lot of ramifications that are being glossed over.
- WalterBright 6y agoThe checks can be at compile time, given how good the data flow analysis is in the compiler. The rest are at runtime, presumably coming with a compiler switch to turn them off. Most people using D leave the checks on. Even without the checks, however, I can vouch that implicitly carrying around the length of the array with the array pointer is a vast improvement in the clarity of the written code.
- prakis 6y agoIt simply impossible for C to finish or vanish. It is one of the most tested and rock solid pillar in programming world. Developers already mastered how to handle the issues you mentioned in article. They are not such big to discard C.
- konjin 6y agoThe first thing you do to see if a new processor has bugs is to port a forth interpreter for it. The first thing you do to see if a new processor is ready for prime time is to port a c compiler for it. The people who talk about rust replacing c are the type of people who thing that GTK is a reasonable C project.
- WalterBright 6y agoI mean it in the sense of starting new development of a major new project with it. Of course, C will be around a very long time, like COBOL and FORTRAN.
- sangnoir 6y agoHow many new embedded projects are picking non-C languages currently? I'm no fan, but C is a long way from dead.
- zucker42 6y agoI would wager the percentage of embedded projects than are picking C has decreased in the past 10 years. I have no direct evidence of that, but I think it's a likely guess.
- knaq 6y agoThe meat of the proposal: "a pair consisting of a pointer to the start of the array, and a size_t of the array dimension" No, that still doesn't fix the ABI. It's syntactic sugar. It is most definitely not passing an array. Passing an array means exactly that, no more and no less. For example, suppose this is your array: double foo[100][100]; The size is 80000 bytes. That is exactly how much data needs to be copied onto the stack, no more and no less. Getting the array dimensions is secondary. It would be nice to have them work. They could automatically get names. They could get size checks, so a function might declare itself compatible with a certain range of sizes. That's all a bonus, of much lower importance than the actual ability to pass an array. The inability to pass an array impacts numerous other languages because they use the C ABI. If you can't put those 80000 bytes on the stack in C, then you can't do it in any language. The whole software ecosystem is thus impoverished.
- brundolf 6y agoAre you non-jokingly suggesting copying the entire array to and from the stack each time, as an alternative to the OP's proposal (and as a default best-practice)?
- knaq 6y agoYes. If you don't really want to pass an array then don't do that. The language shouldn't get in the way when somebody wants to pass an array. Take the address, and pass a pointer, if that is what you want to do. Maybe I want the callee to be able to modify the array without affecting the caller. Maybe I'm even telling the linker to put that array in ROM, but I want a writable copy in the callee. Whatever... I have my reasons. The language shouldn't block me.
- brundolf 6y agoI don't think you realize how intractably inefficient that would be for all but the smallest cases.
- 6y ago
- jokoon 6y agoDo you think there are good contenders to replace C? V seems like it could.
- saagarjha 6y agoV has a bit of a poor track record with its claims.
- geocar 6y agoI hold hopes people will discover as I did, the Real real problem with the C language is the C library and the diglossia. Maybe that's a different language[1], but it's one you get with just a bit of #define and discipline. For example: If you had just put the length before the array buffer, you could've saved a stall in almost every use. That's a problem with out-of-order processing that's hard to fix. Maybe your compiler will get sufficiently smart, or maybe CPUs will collude with the memory controller (or something else amazing will happen), but those things are really hard. However we fixed it ourselves; we didn't need anyone to do it for us, because (due to laziness or luck) C gave us enough of the tools we needed to do what we needed to do. I think that's a bigger deal than buffer overflows, as unpopular an opinion as that is. [1]: https://news.ycombinator.com/item?id=22010895 https://news.ycombinator.com/item?id=22010895
- boomlinde 6y ago> C is finished if it doesn't address the buffer overflow problem Assuming that it is so: when? It seems that C—despite all its shortcomings—remains a very popular language in some problem domains. For some platforms it seems like it's really the only performant HLL option.