6 ms·
>It's funny how both C and LISP programmers seem to suffer from NIH to the point that they'll roll their own just for the heck of it rather than to first see if
by coldtea 4y ago
>It's funny how both C and LISP programmers seem to suffer from NIH to the point that they'll roll their own just for the heck of it rather than to first see if there is a library that they can use.
Well, not sure about the LISP programmers, but C programmers have a good reason: they work under different environments (from embedded to Windows, legacy UNIX, the latest Ubuntu, ...) and also have different needs, regarding allocation, string management, etc. So one-size-fits-all lib might not cut it for everybody. It can also be as simple as having an inherited codebase which uses something else.
Still, there are popular string libraries, and C programmers do use them when they can.
- kaba0 4y agoWhat exactly is a problem with C++’s strings, or especially Rust’s? Everything you mentioned can be controlled as explicitly as you want. The only problem is C is simply not expressive enough to have proper abstractions like that.
- coldtea 4y ago>What exactly is a problem with C++’s strings, or especially Rust’s? Everything you mentioned can be controlled as explicitly as you want. Everything else, however, cannot.
- kaba0 4y agoLike what? C++ and Rust can do everything C can.
- tourmalinetaco 4y agoThey literally cannot. For one, neither C++ or Rust conform to standard C, meaning they already deviate from what C does on a basic level. Their industry uses are highly different as well. C is almost a requirement for embedded systems, and while C++/Rust can be used there, they’re simply too complex and in Rust’s case additionally too young to be adopted. C++, and possibly Rust (if it can get its act together), are more used in high level programming, as that’s what they’re built for. If C++ could do what C can, then why is the Linux kernel 98.5% C code? Wouldn’t it be better to use a more varied and powerful language? Or, maybe different languages have different use cases and cannot be directly slotted in to replace one another.
- tialaramex 4y agoLinux isn't written in standard C. Not only does it not accept C's aliasing rules (hence the kernel is compiled with them disabled, which the standard doesn't offer), it doesn't even accept the memory model, because it had its own memory model first and it likes that one better. As a result to some extent GCC and Clang are also compilers for some sort of "Linux C" which is strongly reminiscent of the ISO standard language but distinct. And there's no reason you would choose C++ for a project where you'd otherwise use C on account of the (exaggerated) relationship between the two languages, the reason you'd do it would be that you want C++ features, and Linus doesn't want C++ features. Projects to "just" compile the existing Linux code but with C++ compilers failed AFAIK.
- shrimp_emoji 4y agoI'll just leave this here: https://lkml.org/lkml/2018/6/5/769 https://lkml.org/lkml/2018/6/5/769 ;p And maybe this: https://dl.acm.org/doi/10.1145/3477113.3487274 https://dl.acm.org/doi/10.1145/3477113.3487274
- flohofwoe 4y agoUnions for type punning is actually entirely valid in C, it's only UB in C++. (I agree though that the C standard isn't actually relevant in the real world, most C compilers treat it more or less as a 'suggestion', unless enforced with options like '-pedantic')
- kaba0 4y agoSo your answer is “because they are not C”…? What does language complexity has to do with anything? It will get compiled down to machine code, and both can be and are used for embedded. They occupy the exact same low-level niche as C, hell, they may be even more level as they can also do things like SIMD. Linux kernel is C because Linus doesn’t like C++, it’s that easy. And usually no, why would you use multiple languages in a project if you don’t have a good reason?
- 4y ago
- scoutt 4y agoLast time I checked "Formatting is Unreasonably Expensive for Embedded Rust" (1) And/or you need a crate like alloc, heapless, etc. For the sake of a sprintf "abstraction"... (1) https://jamesmunns.com/blog/fmt-unreasonably-expensive/ https://jamesmunns.com/blog/fmt-unreasonably-expensive/
- orf 4y agoIt’s not really a sprintf abstraction.
- kjs3 4y agoIf all you do is write code on Linux/Windows/MacOS, C++ strings might be fine. Things are different in the wider world. Many places I use C don't even have a C++ compiler (embedded, in particular).
- flohofwoe 4y agoI don't even know where to start with C++ stdlib string problems, but being mutable and doing a unique heap allocation (above a certain length - a behaviour which however isn't even standardized) are definitely at the top of the list. std::string_view would have been a good thing if it hadn't added another memory corruption foot gun. A universal string type is one of those things where you can either have convenience or performance, but never both.
- pjmlp 4y agoThey are still better than str...() and mem...() all over the place, and all relevant C++ compilers have options to turn bounds checking on.