5 ms·
Just curious: why did you choose C++ instead of C for embedded? Most shops I know chose C just because of code bloat.
by dvfjsdhgfv 9y ago
Just curious: why did you choose C++ instead of C for embedded? Most shops I know chose C just because of code bloat.
- pjmlp 9y agoModern C++ compilers do pretty well on a Commodore 64, let alone many of the typical embedded deployments outside pico-controllers. CppCon 2016: Jason Turner “Rich Code for Tiny Computers: A Simple Commodore 64 Game in C++17” https://www.youtube.com/watch?v=zBkNBP00wJE https://www.youtube.com/watch?v=zBkNBP00wJE Most of the time is either religion against C++ or lack of modern tooling, given that most embedded toolchains are stuck with C90 and C++98.
- abiox 9y ago> religion against C++ possibly. but given the how often i've seen c++ users treat c users like idiot savages or heathens that need conversion ("have you heard the good word of our lord and savior, c++?"), i could understand a negative sentiment.
- pjmlp 9y agoMaybe if C developers wouldn't be ignoring Lint since 1979, and better type systems, we would be having better conversations. "Although the first edition of K&R described most of the rules that brought C's type structure to its present form, many programs written in the older, more relaxed style persisted, and so did compilers that tolerated it. To encourage people to pay more attention to the official language rules, to detect legal but suspicious constructions, and to help find interface mismatches undetectable with simple mechanisms for separate compilation, Steve Johnson adapted his pcc compiler to produce lint [Johnson 79b], which scanned a set of files and remarked on dubious constructions." Dennis M. Ritchie -- https://www.bell-labs.com/usr/dmr/www/chist.html https://www.bell-labs.com/usr/dmr/www/chist.html I would like the stack underlying my computing needs not to look like a Swiss cheese. And yes, C++ is also not the ultimate solution for that as it is tainted by its C compatibility.
- jack_pp 9y agoProbably because it is much safer, faster to develop and easier to maintain.
- kdickens 9y agoIf a new feature does not play well with an entrenched object hierarchy, one can as well do a rewrite. This has happened at a shop where I worked and the rewrite was in C, which turned out to have a faster development time and the result was more flexible. Now, I'm aware that you can restrict yourself to "almost C" in C++, but no one ever seems to be doing that. A litte str::string here, a tiny std::vector there, so exceptions are already in, so why not go all the way.
- CyberDildonics 9y agoIf there is a modern C++ compiler available for a platform, it is almost irresponsible to use straight C. That doesn't mean that everything needs to be pure idiomatic C++17, but destructors, templates, iteration, operator overloading and move semantics are still indispensable in non trivial programs. Avoiding bloat and performance hits are trivial in comparison to structural and architectural benefits in the modern language.
- zwieback 9y agoYou're overstating a bit but there's a kernel of truth. However, it depends on your viewpoint. If you have a functional codebase in C and size is an important factor you may be reluctant to trade up. Bloat avoidance is not trivial, in my opinion. Performance is probably less of a factor, most of the additional functionality generated by the C++ compiler has to be written by the C coder in the end.
- CyberDildonics 9y ago> If you have a functional codebase in C Rewriting something that already works would be silly of course. > and size is an important factor you may be reluctant to trade up. Bloat avoidance is not trivial, in my opinion I don't agree with this in comparison to C. C is still there, but you have destructors and ownership semantics on top of it. Even templates can be used for things like type checking in debug builds.
- cominous 9y agoI have to admit, that C++ is still not the industry "Go-To" language for embedded. But if you apply modern C++ correctly, there is very few overhead compared to C and the software is much easier to maintain. The performance of embedded MCU's are continuously rising over the years and that little overhead is buying development speed. Not to mention smart pointers, templates and constexpr making my life easier. The only real issue with C++ is, that as soon as you get into serious embedded applications, you have restrictions when it comes to heap usage e.g. in medical devices using the heap is forbidden. So you cant use the STL. There is a promising embedded STL project, but it's not there yet: https://www.etlcpp.com https://www.etlcpp.com
- zwieback 9y agoWhen I was using C++ for embedded, which is admittedly over 15 years ago, I had to switch off RTTI and exception handling to get compact binaries. Basically just using classes and surface language features. We did use templates but only very selectively. Is it still possible today to pare down the compiler output like that? I imagine a lot of modern C++ just doesn't work unless everything is enabled.
- pjmlp 9y agoYes it is possible. Check this talk about fitting C++17 on a C64. CppCon 2016: Jason Turner “Rich Code for Tiny Computers: A Simple Commodore 64 Game in C++17” https://www.youtube.com/watch?v=zBkNBP00wJE https://www.youtube.com/watch?v=zBkNBP00wJE Also be aware that embedded devices like Arduino and Cortex-M (with Mbed OS) do use C++ toolchains.
- Const-me 9y ago> So you cant use the STL. std::array is in there since C++ 11 and it doesn’t use heap. And/or you can use STL with custom allocators that work without heap. We did something similar developing for Nintendo Wii console. There was a heap but we didn’t want to use it to avoid memory fragmentation. AFAIR we used two arenas (essentially stacks), one very small for temporary data cleaned up at the start of each frame, and a large one cleaned up when a level is unloaded. However, I don’t have hands on experience developing firmware for medical devices, so I’m not sure it’ll work for them.
- orbifold 9y agoYou can write modern C++ with no overhead on a system with just 16kB of scratchpad memory. It is much nicer to use than C (namespaces, auto, templates and lambdas alone).
- monocasa 9y agoAnd RAII! That's so nice in an RTOS. Never again forget to drop priorities or reenable interrupts just because you went down a not as well trodden failure path. You can even return the lock_guard, and the caller can continue to do work in the same atomically locked context, but if they choose not to, they just drop the return value and everything works as expected.