9 ms·
The problem with C
- quelsolaar 6y agoGood article! I very much agree about C and C++ being philosophically different. When ever someone asks me a C programmer if I want to switch to Rust, I think “but thats a replacement for C++ not C”. I think the ship has sailed on proper compatibly, and thats ok with me, let them be different. More compatibility is not worth braking any existing code.
- MaxBarraclough 6y ago> When ever someone asks me a C programmer if I want to switch to Rust, I think “but thats a replacement for C++ not C”. What do you make of Zig?
- quelsolaar 6y agoI think It has some interesting ideas but many of them dont need a new language. I also think that Zig like C++ thinks that the language is the problem that needs solving. As a C programmer I dont want clever features. I want the simplest possible where nothing happens without me explicitly typing it. I'm much more interested in better debuggers, and tool chains [1]. Every time you create a new language or a significant new version of a language, you have to start over with the tool chain. [1] https://youtu.be/pvkn9Xz-xks https://youtu.be/pvkn9Xz-xks
- SomeoneFromCA 6y agoAs someone who writes a lot of C-like-C++ (system software, network daemons), I absolutely disagree. Using a template or a destructor here and there allows you write C++ code, which is as fast pure C, safer, and easily understandable to a C programmer.
- de_watcher 6y agoUse from C++ just function templates, constants and stricter conversion rules - and you've got a perfect C.
- dysoco 6y agoSadly that way it's just too easy to start using more modern solutions bit by bit and suddenly you are in object-oriented hell or worse. Sometimes I've found that it's just easier to stick with plain C that way you don't ever feel tempted.
- rwmj 6y agoA bunch of modern C programs on Linux are now using __attribute__((cleanup)) which gives you nice cleanups/destructors. You can't compile a modern Linux distro without GCC/Clang support for this attribute.
- pjmlp 6y agoAnd unportable code stuck to GCC C. How ironic that commercial vendors get crucified for C and C++ language extensions.
- SomeoneFromCA 6y agoIt is not even close in terms of functionality to some C++ constructions that emulate Java/C# "finally". Although they are considered "non-canonical" by the C++ purists, I use them often, with great success.
- jstimpfle 6y agoI doubt it's really safer at that point. You could use a few vectors or other container objects, which is a tradeoff - it gets a little bit more comfortable to spot/debug OOB issues (but valgrind and sorts can also help with the occasional problem). In exchange you are required to structure your data and code in a particular way to use these structures, which is a drawback. Another drawback is slower compile times (significantly so with std:: containers -- don't have a lot of experience with hand rolling C++ abstractions). I often prefer having globally allocated (dynamic) arrays, which are unproblematic with respect to OOB or memory/allocation safety. I use a simple macro to wrap allocation of arrays to remove the boilerplate. It becomes like "realloc_array(pptr, FooType, 42)" or even "realloc_array(pptr, 42)". Local allocations are more often not easily dealt with using Memory Arenas, which is superior to alloc/free in constructors/destructor. I even prefer using plain pointers with separate size variables for dynamic arrays. That's because "size :: array" isn't a 1::1 relationship. More than 50% of the time, I have more than one array that applies to the same size variable. Sometimes, there are multiple index/size pairs that apply to a single (or multiple) arrays. I prefer staying with C because it keeps me focused. Sure, sometimes I wish for the occasional fancy feature, but then taking the time to think about a clean approach that solves the problem with limited features often benefits code quality and readability.
- jcelerier 6y ago> I doubt it's really safer at that point. the number of bugs that the compiler has saved me by e.g. having a simple Id<T> type tag instead of ints as used by every C library under the sun is absurd.
- jstimpfle 6y agoI don't know, I've tried approaches like that, even played with writing a few C++ helper functions, and even started a compiler with the goal of exploring ideas like this. How do you go about Id<T>? The way I see that something like this can work out and give small advantages is that T would be an abstract type (a phantom or tag type, or whatever C++ guys are calling it). And all arrays that are meant to be indexed using Id<T> (while the array element types could be whatever) would depend on this type and have to be implemented using a special wrapped array type with overloaded operator[]. I don't know, this might come with many disadvantages. All the stuff goes out the roof when you're now dealing with start indices + slices, i.e. doing "pointer" arithmetics. My general feeling is that this Id<T> approach might show problems similar to the idea of using typed units, like wanting to prevent the expression "1W + 3s" but then making it terribly cumbersome to do real work, e.g. "1W * 3s" which would be perfectly valid. In one C project I resorted to using about 20-30 typedefs like: typedef int Expr; typedef int Symbol; etc. And I used these typedef names in function signatures. Variables also were named like expr, parentExpr, symbol, etc. It worked wonderfully. I had maybe 1 mistake where I submitted the wrong "type" in 6 months. And I'm someone who makes a lot of stupid mistakes.
- InfiniteRand 6y agoA lot of times for a quick program I end up writing essentially C + C++ standard library, because a lot of C++ features related to better organizing of code are overkill when you have only a few files, but avoiding manual string management makes it worth using g++ instead of gcc
- nikki93 6y agoI think you can get far with this type of code organization, and using methods just for easier name lookup of functions associated with a particular type (static lookup -- no vtables / RTTI). Function overload sets are also useful. Libraries like https://github.com/skypjack/entt https://github.com/skypjack/entt really help here too--for an 'ontology of alive objects' its felt like using an entity-component-system has been more ergonomic vs. a big tree of inheritancey classes.
- ddgflorida 6y agoI've always liked plain C better than C++ yet I sure wish they would've had a true string type like other languages. Most bugs originate from the str family of routines.
- Nginx487 6y agoIt's a widely spreaded misconception, that C++ is a superset of C. Nope. Modern C has ton of features, which are impossible in C++, from loosening requirements to implicit type casting (pointers to int) to main() recursion. They are different languages with different niches. I program both in C and C++, and honestly, I don't see modern application of C outside embedded, where C++ compilers implementation is impractical. C++ currently widely used in OS and drivers development, except Linux kernel of course, the only reason of that design approach is personal opinion of Linus Torvalds.
- i-am-curious 6y ago> I don't see modern application of C outside embedded, C is one of the most popular languages for high-performance code. Most famous example would be the Linux kernel but literally anything that needs the best optimizations possible eventually comes down to C.
- ricattierger 6y agoWhy do you say that? Certain things in a language may increase binary size or something else but at the end of the day these languages all go through most likely the same compiler and should produce a roughly equivalent binary.
- hellofunk 6y ago> these languages all go through most likely the same compiler and should produce a roughly equivalent binary That's not really true. A single compiler may support many languages but that does not mean code written in those languages ends up as the same binary.
- bregma 6y agoThey will produce equivalent binaries, but not identical binaries. For example, GCC's C++ front end will produce a different parse tree for the same C code as the C front end, resulting in a slightly different collection of basic blocks and data flow analysis (obviously, for very trivial examples, it will be identical). The result after all the gimplification and and different middle-end passes and rtl transformations you can end up with a surprisingly different set of generated instructions for the same code fed to gcc and g++. Equivalent, but different.
- grive 6y agoIt's weird to conflate C as a language and its ABI, as the glue between other languages. The glue in question is the ABI, period. Rust does not interface with C, it interfaces with a C library (dynamic or static) that is in a standardized format. C++ attempts something messy, which is to take C source and generate objects conforming to this ABI. In doing so, the only sane thing to do is to respect the C spec to transform C source in a binary blob that others can understand. C++ should stop attempting to parse C. Just use a C compiler, create a binary blob and interface with it from the C++ compiler. Attempts to make both language compatible on some common subset makes them both worse off.
- flohofwoe 6y agoIMHO C++ should continue to be able to parse C declarations (e.g. include a C header which only has declarations in it - in fact, all languages with C interoperability should have this feature), but otherwise I agree, C++ doesn't really need to be able to compile C code because (usually) it's trivial to setup a mixed project where C source files are compiled with a C compiler, and C++ source files are compiled with a C++ compiler. This requires that the Microsoft C compiler is updated to the latest C standard though. Until recently their recommendation was to compile C code with the MSVC C++ compiler.
- scatters 6y agoA C header can contain function definitions, though - either using the inline keyword (since C99) or as macros. Moving all functionality behind a function call barrier may not give satisfactory performance.
- flohofwoe 6y agoYeah, adding the inline keyword to C wasn't such a great idea in hindsight because it muddled the separation of interface and implementation (LTO is the better solution IMHO). A possible workaround would be that such a "declaration parser" would ignore the function implementation block (but inline code would make the library unusable from C++ anyway). As for macros: they would simply be resolved by the declaration parser, and either the result is parsable as declarations or not (which would then be an error).
- sys_64738 6y agoA more appropriate question is: The problem with C++ compatibility in C++. The C++ I wrote 20 years ago doesn't look anything like C++ of the modern day. The C is the same.
- colejohnson66 6y agoI’ll admit I’m partial to C++, but couldn’t one argue that that’s because the C committee is just slow to add features to improve the language? I’ll admit, there’s a lot in the C++ standard library, but with zero cost abstractions, if you don’t use, say, `std::map<T,U>`, you don’t pay for it.