News for hackers. Stuff that matters.
Re:Switching to C over 'Modern' Programming Languages by coldtea on Thursday April 13, 2023 @04:53AM (#35552737)
>The very common thing I want to use in C is some sort of variable size string object. But no, I have to dynamically allocate a buffer that I know will be at least the right size for any text I ever put into it, or do I create a buffer that's the correct size for that string but re-alloc if I ever change it to a longer string. But then how do I store the buffer size?

As a C programmer shouldn't you have a library abstracting all this by now? Either your own or one of the dozens available, including pascal-style strings?

Re:Switching to C over 'Modern' Programming Languages by jacquesm on Thursday April 13, 2023 @04:57AM (#35552770)
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.

The long term cost of those decisions as well as the number of really bad bugs (and security issues) that can be traced back to one-off code is likely much larger than the same figure for well used libraries. But it all sort of evens out whenever a bug in such a library is found because then it is so widespread that lots of systems will suffer.

Weird how that works.

Re:Switching to C over 'Modern' Programming Languages by coldtea on Thursday April 13, 2023 @05:16AM (#35552920)
>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.

Re:Switching to C over 'Modern' Programming Languages by kaba0 on Thursday April 13, 2023 @05:27AM (#35552999)
What 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.

Re:Switching to C over 'Modern' Programming Languages by coldtea on Thursday April 13, 2023 @06:35AM (#35553429)
>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.

Re:Switching to C over 'Modern' Programming Languages by kaba0 on Thursday April 13, 2023 @06:50AM (#35553537)
Like what? C++ and Rust can do everything C can.
Re:Switching to C over 'Modern' Programming Languages by tourmalinetaco on Thursday April 13, 2023 @08:17AM (#35554266)
They 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.

Re:Switching to C over 'Modern' Programming Languages by tialaramex on Thursday April 13, 2023 @09:03AM (#35554777)
Linux 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.

Re:Switching to C over 'Modern' Programming Languages by shrimp_emoji on Thursday April 13, 2023 @09:57AM (#35555537)
Re:Switching to C over 'Modern' Programming Languages by flohofwoe on Friday April 14, 2023 @03:36AM (#35566601)
Unions 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')

Re:Switching to C over 'Modern' Programming Languages by kaba0 on Thursday April 13, 2023 @09:11AM (#35554883)
So 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?

Re:Switching to C over 'Modern' Programming Languages by cozzyd on Thursday April 13, 2023 @09:55AM (#35555509)
It is quite difficult to write idiomatic C++ without memory allocation, for one, which is often a requirement for embedded code or any high-availability code that is not allowed to have memory fragmentation.

Sure, it's possible (you can write C in C++, for the most part, and to its credit, C++ has placement new). But many of C++'s niceties require memory allocation, making much of its value-add over C questionable.

Re:Switching to C over 'Modern' Programming Languages by pjmlp on Thursday April 13, 2023 @03:30PM (#35560914)
It super easy, we did it all the time back in the MS-DOS days.
Re:Switching to C over 'Modern' Programming Languages by cozzyd on Thursday April 13, 2023 @05:30PM (#35562328)
right, but that was pre C++ niceties like the STL :).
Re:Switching to C over 'Modern' Programming Languages by pjmlp on Friday April 14, 2023 @02:20AM (#35566133)
C++ compilers shipped their own collections, for Borland compilers it was BIDS.

The first version used preprocessor macro tricks for generatic code, basically what Go folks re-discovered with //go:generate, we already had it in 1992.

BIDS 2.0 already used templates.

It was exactly because even C++ for MS-DOS provided safer library than C, that I was never that into C. It wasn't better than TP in features and safety, only in portability, and that I could get from C++ anyway.

Re:Switching to C over 'Modern' Programming Languages by cozzyd on Friday April 14, 2023 @11:52AM (#35570926)
That's fair! Perhaps I should amend to say that "current" idiomatic (almost got autocorrected to idiotic on my phone) c++ code is allocation heavy. Probably there exist suitable libraries I don't know about that basically implement the STL using predefined arenas for storage.
Re:Switching to C over 'Modern' Programming Languages by scoutt on Thursday April 13, 2023 @09:43AM (#35555318)
Last 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/

Re:Switching to C over 'Modern' Programming Languages by orf on Friday April 14, 2023 @02:25PM (#35572967)
It’s not really a sprintf abstraction.
Re:Switching to C over 'Modern' Programming Languages by kjs3 on Thursday April 13, 2023 @10:07AM (#35555666)
If 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).
Re:Switching to C over 'Modern' Programming Languages by flohofwoe on Friday April 14, 2023 @03:29AM (#35566554)
I 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) 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 general string type is one of those things where you can either have convenience or performance, but never both.

Re:Switching to C over 'Modern' Programming Languages by pjmlp on Friday April 14, 2023 @04:56AM (#35567139)
They are still better than str...() and mem...() all over the place, and all relevant C++ compilers have options to turn bounds checking on.
Re:Switching to C over 'Modern' Programming Languages by flashgordon on Thursday April 13, 2023 @11:41AM (#35557159)
My c knowledge is obviously old. I wonder if the author is lamenting the lack of a "modern" string manipulation in the standard library beyond just working on char buffers?
Re:Switching to C over 'Modern' Programming Languages by xigoi on Thursday April 13, 2023 @03:59PM (#35561306)
Okay, so you write your own string library. Now you'd like to do the same thing for resizable arrays, so you write a resizable array li… oops, you can't, because C doesn't have parametric types.
Re:Switching to C over 'Modern' Programming Languages by flohofwoe on Friday April 14, 2023 @03:13AM (#35566454)
Preprocessor macros are an entirely valid tool in the C language toolbox, even if demonized by C++ coders.
Re:Switching to C over 'Modern' Programming Languages by pjmlp on Friday April 14, 2023 @04:54AM (#35567129)
Even Macro Assemblers from the MS-DOS and Amiga days are better than C pre-processor macros.
Re:Switching to C over 'Modern' Programming Languages by melagonster on Friday April 14, 2023 @02:06PM (#35572679)
it is black magic, absolutely respect C developers.
Re:Switching to C over 'Modern' Programming Languages by flohofwoe on Saturday April 15, 2023 @05:58AM (#35579127)
But the preprocessor is just simple text replacement, nothing magic about it.
Re:Switching to C over 'Modern' Programming Languages by tpoacher on Friday April 14, 2023 @04:42PM (#35574466)
He didn't say "write your own library". He said "use" one (your own if you prefer). Or are you going to suggest there are no good string handling libraries for C?
Re:Switching to C over 'Modern' Programming Languages by xigoi on Friday April 14, 2023 @06:44PM (#35575822)
It doesn't matter. My point is that it's not possible, not for you and not for anyone else, to implement a type-safe generic resizable array library in C.