4 ms·
Still, the thing is, I really don't buy into any arguments in favor of starting a new project, in C, today. The problem is that C suffers from some really seri
by charles-salvia 10y ago
Still, the thing is, I really don't buy into any arguments in favor of starting a new project, in C, today. The problem is that C suffers from some really serious disadvantages as a systems programming language, and it has no significant advantages over C++ or Rust, apart from subjective ideas like "it's simpler" or circumstantial issues like "my team doesn't know C++".
Firstly, apart from a simple call stack, C has no automatic resource management. There are no automatic destructors. Thus it is too easy to forget to free some memory or unlock a mutex or decrement a ref count or whatever, especially when many code paths are possible after a resource has been allocated. The other options are to use the "goto cleanup" or "goto err" idiom, which is still way more error prone than automatic destructors.
And secondly, despite being touted as so "efficient", C offers no way to write code that is both generic and free of runtime overhead, apart from preprocessor macros (which are filled with their own issues), or automatic code generation scripts. Both C++ and Rust have metaprogramming facilities which provide the ability to write generic code without sacrificing performance or maintainability.
I mean, with C++, the automatic deterministic allocation/destruction alone is worth using it over C. And all of the downsides (larger binaries, etc.) can be overcome without much effort. I concede that the extra language complexity of C++ over C could be a disadvantage, but in my mind that doesn't outweigh the benefits.
- humanrebar 10y agoI mostly agree, but C compiles in terms of a binary interface, which makes it unique in it's ability to provide language agnostic libraries. You can do that in C++, but by wrapping your C++ in C, more or less. Rust might provide a compelling alternative here eventually, but it's still a young language.
- catdog 10y agoProviding no stable ABI and not being easy to bind to in general is really a major pain point of C++ for library/framework code. In contrast everyone and their dog is able to call into C in practice which is, I assume, a not to be underestimated reason why it is still so popular, even for code not leveraging its low level nature. Rust is really interesting here as they have chosen to make it more than an afterthought to provide compatibility. > I concede that the extra language complexity of C++ over C could be a disadvantage, but in my mind that doesn't outweigh the benefits. It's a problem, C++ is so insanely complex, full of magic which is hard to grasp and provides more ways to shoot yourself in the foot than to do it right (modern C++ seems better in some regards but still…). C is often chosen because people want sth. reasonable simple so I think a language fitting in between, being richer than C but still fairly comprehensible would be nice.
- stephen82 10y agoThe size of a C++ binary file that has been constructed with a modern, up to date compiler can produce the same size as that of C. I have tested this a year ago with both clang and GCC, and the optimized, stripped down binaries were identical in size.