News for hackers. Stuff that matters.
Re:Modern C++ for C Programmers (2018) by ipnon on Friday July 23, 2021 @07:20PM (#27936782)
Is Rust preferable for the embedded systems engineer who is primarily concerned with being modern?
Re:Modern C++ for C Programmers (2018) by epistasis on Friday July 23, 2021 @08:38PM (#27937381)
I'm not a systems engineer, I just do data analysis and algorithms that sometimes need to go really fast.

I only recently investigated rust, and was hugely impressed with it. I regret not investigating it earlier. Language support for ownership and lifetime of variables is such a huge step up for writing code. Trying to do this through as hoc conventions in C, or through RAII in C++, is a lot harder.

I haven't yet had a chance to write anything significant in rust beyond a few small test projects, but next time I want to beat Python performance on something I'll be using Rust instead of Cython or C or C++.

Re:Modern C++ for C Programmers (2018) by brooksbp on Friday July 23, 2021 @10:09PM (#27937837)
It gets real fun when "going fast" is less about the software and more about what's happening inside the chip!
Re:Modern C++ for C Programmers (2018) by Bayart on Friday July 23, 2021 @08:32PM (#27937346)
I don't know that « being modern » should ever be a primary concern. Especially when it comes to embedded where stable features and APIs are important.

It's largely the realm of C, with some industries working in Ada/SPARK. In terms of its profile, Rust fits the bill for being a good system/embedded programming language some day, but I don't know if it's mature and stable enough yet to be what you'd pick for a really serious job.

As for C++, I don't really see the point if you don't have technical debt.

Re:Modern C++ for C Programmers (2018) by pjmlp on Saturday July 24, 2021 @11:13AM (#27941603)
Some embedded standards like AUTOSAR, require C++ not C.
Re:Modern C++ for C Programmers (2018) by CraigJPerry on Friday July 23, 2021 @07:50PM (#27937069)
For embedded, i would have said C not C++. C is more prevalent still.

For some industries where security or correctness guarantees are necessary, you see things like MISRA C used rather than Rust (or cpp).

Re:Modern C++ for C Programmers (2018) by michaelsbradley on Friday July 23, 2021 @08:10PM (#27937202)
There's also the Ravenscar profile[1] for Ada and the SPARK language[2]. How those compare in marketshare to MIRSA C, I'm not sure, I'm not in that field, though I've enjoyed reading about them at a conceptual level.

[1] https://en.wikipedia.org/wiki/Ravenscar_profile

[2] https://en.wikipedia.org/wiki/SPARK_(programming_language)

[&] https://docs.adacore.com/sparkdocs-docs/Examiner_Ravenscar.h...

Re:Modern C++ for C Programmers (2018) by oddity on Friday July 23, 2021 @08:21PM (#27937272)
Anecdotally, everything I've seen (hardware drivers) has been in (MISRA) C or a subset of C++. Ironically, many of the alternatives require too much discipline to keep devs happy. MISRA C at least lets them pretend they're in their happy place.
Re:Modern C++ for C Programmers (2018) by dexterhaslem on Saturday July 24, 2021 @01:00AM (#27938514)
ive been in the misra c realm a while and romanticized ada, spark, ravenscar profile etc. i think if you have a team and problem that can go 100% in on it, its unmatched (and some industries certainly do, aviation, autopilots, internal fuze controls etc..)
Re:Modern C++ for C Programmers (2018) by sharikone on Friday July 23, 2021 @08:58PM (#27937493)
I guess so. Some newer libraries, especially if they target "modern backend" stuff like webassembly, websockets, ... are written in Rust. When safety is a concern it could be really preferable.

But, say, if you don't have a good support for your platform Rust is not useful. Ditto for most existing codebases. And in any case I don't see why your primary concern should be being modern vs getting things to work well

Re:Modern C++ for C Programmers (2018) by osivertsson on Friday July 23, 2021 @10:11PM (#27937845)
Most places I’ve done embedded dev at have been C or C++. Actually all, with some Lua or assembler sprinkled in.

Debugging C++ when your compiler for weird target (SH4, or worse) contains bugs, or when you have some relaxed memory consistency model, is quite a challenge because mapping C++ to assembler is definitely non-trivial. C, even with heavy optimizations, is a breeze comparatively.

I would happily join a team that uses Rust in an embedded setting. It shows that they care deeply about doing better than average, and that probably means they are better than average in everything.

Re:Modern C++ for C Programmers (2018) by ncmncm on Friday July 23, 2021 @11:04PM (#27938077)
Mapping C++ to assembler is exactly the same as mapping C to assembler, for those operations the languages share. If you are coding reasonably, there aren't any other operations that happen at runtime. C++ does its heavy lifting, preventing bugs, at compile time.
Re:Modern C++ for C Programmers (2018) by osivertsson on Saturday July 24, 2021 @02:57AM (#27938997)
For sure C++ does a lot of heavy lifting at compile time, which shows in poor compile and link times, although it is less of a problem today than 25 years ago since hardware performance has increased.

I don’t agree that C++ automatically prevents bugs, even though using e.g. RAII can help a bit and leads to maintainable code.

If you use exceptions (you probably shouldn’t) there is one more code-path out from your code. That together with constructors/destructors being called and using STL instead of more trivial data-structures often leads to a lot bigger assembler output to trawl through if you need to.

Re:Modern C++ for C Programmers (2018) by ncmncm on Saturday July 24, 2021 @04:35PM (#27944250)
C++ doesn't "automatically" prevent bugs. You have to actually, you know, use it.

By STL I guess you mean std::string and std::vector. That is wholly optional. I would not use those in a small embedded project, except maybe at startup, and then with a custom, non-freeing bump allocator.

Re:Modern C++ for C Programmers (2018) by oddity on Friday July 23, 2021 @08:09PM (#27937195)
(I'm assuming you're coming from C and don't already know C++ because of the linked article) If the ecosystem that's available is sufficient for your needs on the platforms you care about and you're not interested in easy compatibility with a C/C++ codebase or your existing C knowledge, I would recommend Rust over C++ whenever possible (C is a different question since it fills a slightly different niche and doesn't really have a solid alternative yet imo). Rust has a much simpler and well-chosen set and interaction of features than C++, but it does that by dropping many of the compatibility constraints of C and C++ (and making assumptions that are easier to make in 2021 than 30+ years ago). Compiler errors also more honestly reflect the learning curve, so it can feel more punishing at the start, but has a shorter learning curve to reach functionally correct code, where with C++ you might easily have been using it for 10+ years and still learn you've been doing something wrong the whole time.

That's a big set of ifs, though. It's possible that the compiler errors are discouraging enough that C++ might feel like a more comfortable choice (I would encourage you to not let this be your reason unless you're just interested in solving a one-time problem). I keep checking in on Rust to see how small and dependency free I can get the compiled binary, and I usually need a mess of nightly features. The ecosystem is also still a mess. There are a few great libraries, but otherwise it's a minefield of 0.x releases for most dependencies (admittedly, some of the most important ones are autogenerated bindings where the version number is meaningless). Bindings are really easy to write with Rust, however, and it's one of the few languages that really gets that right, but it's nowhere near as convenient as calling/using C or C++ from C++. Most of these issues are consequences of C and C++ having the existing inertia, but it is what it is. Some people want to ship things and not join a community.

If you're trying to get a job or just learn: learn both. C++ isn't going to go away completely any time soon, but, with every month that passes, there are fewer and fewer technical reasons to justify starting a new project in it vs Rust. Choosing an inferior tool because it's the only one you understand might often be the correct choice, but it's not a choice you want to have to make.

Re:Modern C++ for C Programmers (2018) by ncmncm on Friday July 23, 2021 @10:17PM (#27937866)
C++ is not only not going away, its usage is still growing very, very fast. More people pick up C++ for professional use in any given week than the total who are coding Rust at work today. That will be true next year, too. All of the highest-paying coding jobs use C++. That will be true next year, too.
Re:Modern C++ for C Programmers (2018) by ipnon on Friday July 23, 2021 @08:34PM (#27937357)
Thank you for this thoughtful comment. It seems the present moment is a flux for embedded programming languages, there is still a choice to make. Maybe we will be discussing "Modern Rust for C++ Programmers" in a few decades on HN!
Re:Modern C++ for C Programmers (2018) by dmitrygr on Friday July 23, 2021 @07:46PM (#27937029)
Why is "being modern" a concern? Luckily, embedded is not the web, where 5000 frameworks sprout over lunch. I've never heard of anyone in embedded caring about being modern. Only about solving the problems efficiently. So i am curious: why do you ask?
Re:Modern C++ for C Programmers (2018) by ipnon on Friday July 23, 2021 @08:25PM (#27937300)
Embedded engineering seems to be a lost art in the America. The typical American programmer is certainly not an embedded engineer, and our industry and American universities focus mainly on the application layer of both practice and theory. My university's[a] computer architecture and operating systems courses are both taught entirely using simulation, either simulated hardware or virtual machines, and we would certainly never write our own bootloaders or really ever put code to the metal. It all seems more Shenzhen than Silicon Valley, like part of the stack is simply a bygone thing of the past for American software engineers.

Neither C nor C++ could be considered new languages, and this seems to contribute to this estimation of embedded engineering as a "lost art." C and C++ give a "here be dragons feeling," that only the best engineers can hope to avoid pointer pitfalls and memory leaks. I wonder if Rust's advances in programming language theory could alleviate this problem. The new generation of programmers I feel I belong to are comfortable with functional programming, advanced types, and facile tooling like NPM.

I have personally found embedded engineering underrated in American industry, both for fun and profit. If adopting modern practices leads to further embedded engineering adoption, that would be a worthwhile pursuit for individuals and our community. Perhaps this takes the form of Rust.

[a] Top 300 in global ranking, with a standard but not cutting edge computer science program unlike Stanford or CMU. Having gone to only one university I admit this confusion could simply be a failing of my own education.

Re:Modern C++ for C Programmers (2018) by iainmerrick on Saturday July 24, 2021 @03:51AM (#27939139)
I think embedded programming is similar to games programming -- it’s difficult due to the performance constraints and niche skills required (low-level I/O, 3D graphics), but also relatively badly paid. Edit to add: admittedly for different reasons; in gaming I think it’s that the supply of programmers outstrips demand, as every other new graduate dreams of being a games programmer.

I think the cultural shift is partly just people voting with their feet and going where the money is.

Re:Modern C++ for C Programmers (2018) by ipnon on Monday July 26, 2021 @12:42AM (#27955748)
America certainly has the engineering talent, it's really the manufacturing base that has been decimated. In some sense this migration was necessary for the iPhone and web app revolutions, Web 2.0. Perhaps we'll see embedded engineering and manufacturing return to America once web and mobile application development becomes over saturated. Already there seems to be a struggle for normal new grads to enter these fields.
Re:Modern C++ for C Programmers (2018) by dmitrygr on Friday July 23, 2021 @08:39PM (#27937388)
Embedded has always been a "I learned this myself, while tinkering, then expanded my knowledge by tinkering more, and then by having a job doing something similar" kind of field. So that is not new.
Re:Modern C++ for C Programmers (2018) by flyingcircus3 on Saturday July 24, 2021 @12:49AM (#27938478)
Another significant difference in embedded development is that it's the intersection of electrical engineering and software development. Without an understanding of both disciplines, it's difficult to be effective in a professional setting. Understanding the hardware, which is almost always custom to your project, is a base level requirement to be able to write the software that will control it.
Re:Modern C++ for C Programmers (2018) by dexterhaslem on Saturday July 24, 2021 @01:02AM (#27938535)
very true, i found a niche here (well 70% firmware but being able to understand and reason about the big picture of the rest) and really enjoyed it. seemed to have carved a niche out too troubleshooting basic hw level issues and delivering faster. highly recommend it to anyone who feels it isnt applicable
Re:Modern C++ for C Programmers (2018) by ipnon on Saturday July 24, 2021 @02:55AM (#27938989)
Would you mind naming your company? Or a high profile company in the same field as your work? I'd like to get a taste of the sort of work you do.
Re:Modern C++ for C Programmers (2018) by ncmncm on Friday July 23, 2021 @10:07PM (#27937830)
Embedded programming in C is a chore, because embedded code can be much trickier to debug. Writing C without bugs is a great deal of work, and very stressful. Debugging it when you don't is also very stressful.

Embedded programming in C++ is pure fun. The more modern your code is, the less room you leave for bugs: when it compiles, it mostly works. Where it doesn't, it shows up in your logic, not in random memory corruption.

Re:Modern C++ for C Programmers (2018) by beached_whale on Friday July 23, 2021 @10:18PM (#27937870)
I do a lot of static_asserts and small composable constexpr methods so that when I am wrong I get a compile failure. Actually with IDE's I get a little overlay telling me the error usually.

Constexpr methods seem perfect for embedded in a lot of ways too. Yes, I won't be touching real memory or ports, but I can ensure that the method isn't doing silly things either. Having a memory address class that filters on the proper operations and only allows them is awesome. Saw a good video on the topic recently too https://www.youtube.com/watch?v=fsMmh8F8uV0

Re:Modern C++ for C Programmers (2018) by osivertsson on Friday July 23, 2021 @10:24PM (#27937891)
Except that most devs don’t understand what their C++ code actually does at runtime and many try to be too clever and use too many features. I have seen spectacular failures regarding memory usage and copies, static initializers, even exceptions (just design your APIs without them!)

C++ code bases may have less buffer overflows and off-by-one errors but I don’t trust them unless they come with an extensive safety-net in the form of unit-tests with good coverage.

Re:Modern C++ for C Programmers (2018) by gumby on Friday July 23, 2021 @10:14PM (#27937855)
Forget the word “modern” — its just a label and the word “orange” could just as well be used.

It refers to an approach that doesn’t worry much about how things were done in C and instead thinks about how to represent things cleanly and efficiently with the aim of faster development and fewer bugs.

I was an embedded developer for years and still write a lot of code in C++ because it results in fast code that is easier to write than if I tried in C.

One of mantras of the c++ committee is that you should pay (have overhead) for the stuff you don’t use. So I don’t try to write code that looks like C.

Re:Modern C++ for C Programmers (2018) by maxk42 on Friday July 23, 2021 @07:24PM (#27936815)
Being modern for its own sake? I suppose it would be preferable, since Rust is newer and evolving rapidly. But if you're concerned with being employed and a wide variety of powerful tooling at your fingertips then C++ is probably a better bet. (Note: I am neither a C++ nor Rust developer, so I don't have a dog in this fight.)
Re:Modern C++ for C Programmers (2018) by dexterhaslem on Saturday July 24, 2021 @12:57AM (#27938504)
in my embedded experience (small scrappy teams in contracting) c99 is just barely common place. and very welcome. 99% covers everything I've done. but for absolutely huge mcus where rust is supported, rust is probably welcome and easier to fit
Re:Modern C++ for C Programmers (2018) by adamnemecek on Friday July 23, 2021 @07:32PM (#27936908)
Rust is always preferable.
Re:Modern C++ for C Programmers (2018) by rvz on Friday July 23, 2021 @10:07PM (#27937829)
Too bad there is no first-class production ready GUI library for Rust that I can use for embedded systems.

Qt and C++ it is then.

Re:Modern C++ for C Programmers (2018) by pjmlp on Saturday July 24, 2021 @11:41AM (#27941830)
And on the context of Windows IoT, UWP is all about .NET and C++, with the Rust bindings pretty much still doing baby steps.
Re:Modern C++ for C Programmers (2018) by netr0ute on Friday July 23, 2021 @07:37PM (#27936951)
On what merits?
Re:Modern C++ for C Programmers (2018) by Inityx on Friday July 23, 2021 @07:47PM (#27937036)
Memory safety, build system, dependency management, the usual
Re:Modern C++ for C Programmers (2018) by adamnemecek on Friday July 23, 2021 @07:50PM (#27937068)
It's just overall productivity.
Re:Modern C++ for C Programmers (2018) by netr0ute on Friday July 23, 2021 @07:51PM (#27937075)
Can you define productivity?
Re:Modern C++ for C Programmers (2018) by pornel on Saturday July 24, 2021 @06:26AM (#27939698)
For example, I almost never need to use a debugger or Valgrind with Rust. Mysterious memory corruption just isn't a thing. Memory leaks are not a problem either (even though technically Rust doesn't claim to prevent all leaks, it does well enough in practice).

Rust catches data races, use of non-thread-safe functions, and use-after-free problems across the entire codebase, including (mis)use of 3rd party libraries, at compile time. I can add `.par_iter()` to a loop, and if it compiles, I know it doesn't mangle any data. If it doesn't compile, the compiler points me to the exact struct that needs a refcount, or a mutex, or atomic. That saves me weeks of chasing heisenbugs.

In debug builds it checks all integer arithmetic for overflow (no need to wrap types in custom class, works for dependencies too). This has saved my ass a few times.

There's a pretty decent standard library, and a good number of quality dependencies that can be installed with a 1-liner. If I need to (de)serialize a struct from JSON, it's a couple of lines. I want to switch that to msgpack instead, 2-line change. I need that compressed? 2 lines again, plugs in easily, and supports serialize-and-compress without an intermediate buffer.

Build system uses convention over configuration, so there's no artisanal "build script" to manage, or even an argument which build system sucks less. Dependency management and workspaces work well enough that it's practical to modularize projects into smaller libraries, and actually reuse them. Unit testing is built-in. Doxygen-like generator is also built-in, no config needed. Of course any language can do the same, but having zero friction removes excuses and really helps making people actually use these things.

Re:Modern C++ for C Programmers (2018) by adamnemecek on Saturday July 24, 2021 @11:14AM (#27941611)
It's just such a step-up that it's not even comparable.
Re:Modern C++ for C Programmers (2018) by smoldesu on Friday July 23, 2021 @07:55PM (#27937110)
I can't speak for everyone, but I spent an unconscionable amount of time managing dependencies and libraries in C++. In Rust, it's the easiest part of my workflow.
Re:Modern C++ for C Programmers (2018) by netr0ute on Friday July 23, 2021 @08:11PM (#27937207)
What would be best is libraries that can be added with a one-liner of an include statement, then there's no more dependency management needed.
Re:Modern C++ for C Programmers (2018) by adamnemecek on Friday July 23, 2021 @08:04PM (#27937165)
You can speak for everyone. C++ dependency management is just...I don't even know where to begin.
Re:Modern C++ for C Programmers (2018) by dkersten on Friday July 23, 2021 @08:30PM (#27937333)
> You can speak for everyone.

Not really. I only use header-only libraries and dependency management is a breeze for me. I check each library out as a git submodule into a third-party subdirectory in my project directory, point the includes path at it and off I go. When I want to upgrade, I pull the git tag/branch/commit I want and recompile. Its pretty simple.

Re:Modern C++ for C Programmers (2018) by adamnemecek on Friday July 23, 2021 @09:19PM (#27937590)
I mean that's cool but you are definitely working against the language as opposed to having the language support you. Also this works nicely for self contained dependencies, and much less nicely for not self contained dependencies.
Re:Modern C++ for C Programmers (2018) by dkersten on Friday July 23, 2021 @09:41PM (#27937703)
> you are definitely working against the language

How so?

> Also this works nicely for self contained dependencies, and much less nicely for not self contained dependencies.

Doesn't matter. You said: "You can speak for everyone." which is simply not true, as I have outlined. You didn't say anything about not self contained dependencies or whatnot. The person you replied to said "I spent an unconscionable amount of time managing dependencies and libraries in C++" and I... well, don't. So clearly not everybody does.

Re:Modern C++ for C Programmers (2018) by adamnemecek on Friday July 23, 2021 @09:51PM (#27937752)
Right, but not all problems can solved with header only frameworks.
Re:Modern C++ for C Programmers (2018) by dkersten on Friday July 23, 2021 @11:16PM (#27938115)
I did use Bullet physics not too long ago, which most definitely isn't header-only, but the exact same approach worked there too. Git submodule, set include path. The only difference is that I also told my build tool to build its sources and package them into an .a archive, which I linked against. It was no trouble at all.
Re:Modern C++ for C Programmers (2018) by dkersten on Saturday July 24, 2021 @07:55AM (#27940130)
Too late to edit: yes, I know this doesn't work for everything and won't work for everyone, but its worked great for me, so my point is simply that your assertion that everyone has difficulties dealing with dependencies is false. Some people for sure do, but there are people, like me, who don't.
Re:Modern C++ for C Programmers (2018) by jimbob45 on Friday July 23, 2021 @07:50PM (#27937064)
Is there anything C++ does better at this point? Outside of compatibility, that is.