News for hackers. Stuff that matters.
Re:Some Were Meant For C (2017) [pdf] by ncmncm on Monday March 01, 2021 @02:24PM (#26307041)
The article mentions C++ three times, all in contexts equating it with C. But C++, as it is coded today, is a very different language from C, and does not suffer from the problems that make C an extremely poor choice for starting any new project that might matter.

All the article's arguments for C apply substantially moreso to C++. Thus, the article leaves us with no objectively plausible reason ever to code in C, except where artificial constraints mandate it, or where merit doesn't matter. (I leave to the reader to decide where Linux and BSD kernels fit in that.)

There is especially no excuse for systemd to be coded in C.

Re:Some Were Meant For C (2017) [pdf] by qznc on Monday March 01, 2021 @02:52PM (#26307477)
C++ still has a lot of rope to hang yourself. For example, there is the stuff Rusts borrow checker prevents. Or that members are initialized by declaration order and a wrong initializer list can lead to undefined behavior. Or all those implicit conversion rules (ok, the annoying tendency to convert to int is inherited from C).

The most ugly aspect is that C++ has a lock-in effect like Whatsapp. As long as your codebase is in C, you can easily interface with nearly all other languages. Once important parts are in C++ though, only C++ can reasonably interface with it.

Re:Some Were Meant For C (2017) [pdf] by ncmncm on Monday March 01, 2021 @03:39PM (#26308081)
If "lock-in" is something wrong with C++, it is equally so with Rust. But in fact anything you want callable from C, in either C++ or Rust, is easy to keep that way.

To the other point, you can write bad code in any language, Rust included. But you don't have to. A language can help by making good code easier to write than bad code. C fails so frequently by making good code much harder to write, instead.

Re:Some Were Meant For C (2017) [pdf] by sweeneyrod on Monday March 01, 2021 @04:55PM (#26308981)
C++ is about 17 languages
Re:Some Were Meant For C (2017) [pdf] by Koshkin on Monday March 01, 2021 @06:09PM (#26309774)
That’s C++17 you are talking about.
Re:Some Were Meant For C (2017) [pdf] by rurban on Tuesday March 02, 2021 @07:01AM (#26314379)
Nope. Ancient C++, c++98, 03, 11, 14, 17, 20

I count 7, plus msvc, which adds another one. 8

Re:Some Were Meant For C (2017) [pdf] by coliveira on Monday March 01, 2021 @02:45PM (#26307379)
I disagree. First of all, most C code can be run as C++, so everything bad you can say about C applies to C++ as well. Second, C++ introduces its own problems that also make programming unsafe compared to other managed languages and even compared to C.
Re:Some Were Meant For C (2017) [pdf] by qznc on Monday March 01, 2021 @02:55PM (#26307519)
You would constraint yourself to a subset of C++ which is not C. For example, forbid all raw pointers and only use smart pointers. Now your memory is freed automatically via RAII and use-after-free errors are much harder to create.
Re:Some Were Meant For C (2017) [pdf] by coliveira on Monday March 01, 2021 @03:11PM (#26307740)
As you said, this is a subset of C++. But everyone debates what the right subset should be. In reality C++ is incapable of solving the problem, it can only propose good practices.
Re:Some Were Meant For C (2017) [pdf] by aromatic_dev on Monday March 01, 2021 @05:33PM (#26309396)
Though if one only uses smart pointers they will have to accept the performance hit that that brings -- in high performance situations, this may be unacceptable.
Re:Some Were Meant For C (2017) [pdf] by ncmncm on Monday March 01, 2021 @06:43PM (#26310070)
Circumstances where using smart pointers affects performance are vanishingly rare. Where performance matters, on any critical path no allocation or deallocation is done, so a smart pointer can have no effect. On other paths, any difference is swamped by overhead in malloc and free.

So, in actual practice, no.

Re:Some Were Meant For C (2017) [pdf] by wheybags on Monday March 01, 2021 @06:58PM (#26310196)
There's some argument to be made about bookkeeping and (maybe? I'm not sure) an extra level of indirection with shared_ptr, but the cost of unique_ptr is literally zero.
Re:Some Were Meant For C (2017) [pdf] by ncmncm on Monday March 01, 2021 @08:43PM (#26310925)
Passing unique_ptr by move works out to be surprisingly expensive, so one doesn't, where that matters. (It is usually a mistake to have unique_ptr visible in an interface at all, but passing a pimpl type, implemented with unique_ptr, by move costs exactly the same.)

And of course no one who cares for their reputation is seen using shared_ptr.

Re:Some Were Meant For C (2017) [pdf] by boring_twenties on Tuesday March 02, 2021 @12:22AM (#26312155)
What's the expense of moving unique_ptr -- is it resetting the source to nullptr?
Re:Some Were Meant For C (2017) [pdf] by mawww on Tuesday March 02, 2021 @01:35AM (#26312480)
My guess is that because in most common x86_64 calling conventions the caller is responsible for destructing the parameters, it has to run the unique_ptr parameter destructor which needs to check if it was moved from in the callee or not. Additionally, because this destructor is not a trivial one (i.e. it does something), unique_ptr cannot be passed directly through a register but must be spilled on the stack.
Re:Some Were Meant For C (2017) [pdf] by ncmncm on Tuesday March 02, 2021 @09:40AM (#26315754)
This is correct. Chandler Carruth gave a talk about unfortunate pessimizations imposed by the ABI. Where the call is inlined, that stuff is optimized out, but functions that take a moved unique_ptr or pimpl object -- taking ownership of a heap object -- tend not to be inlined unless they mostly just pass it along to one that isn't inlined.

So, the cost of moving a smart pointer onto the stack and, presumably, off again to someplace less ephemeral might matter, on a critical path. But it would be a mistake to exaggerate this: we are talking about a couple of unfortunate ordinary memory operations, not traps, function calls, or atomic synchronizations, when you might have hoped to be using just registers. If you are already touching memory -- something pretty common -- it is just more of that.

Re:Some Were Meant For C (2017) [pdf] by wheybags on Tuesday March 02, 2021 @10:03AM (#26315995)
Wow, did not know this, I'm going to go and have a deeper look at it now. Thanks to all in this comment thread!