3 ms·
Ask HN: Use C with Classes for Embedded?
Hello. I am writing code for embedded systems. Most cpu are ARM or SoftCpu from FPGA vendors. Up to now I use C11. All the CPU are supported by gcc, thank god. I used to write c++ before C++ 11. The good old time. I now think more and more often to switch to C++ on embedded. No exceptions no RTTI no STL or Boost. Just C with Classes for the luxury of C++. What do you think? Is it worth the hazel?
- Disposal8433 1y agoI have a small experience with embedded systems but I once helped a team of hardware engineers switch from a "assembly-like code with loops and state machines" to C++17/20 on a very barebone platform and everyone was happy with the improvements in code quality and tools. IIRC we even used relatively advanced objects like `std::optional`. No need to shun the STL as it supposedly compiles to small code most of the time.
- rzzzwilson 1y agoWhy not? Just stay away from anything that may allocate/free memory after system initialization, like "String".
- Surac 1y agoDynamic memory is forbidden in my applications anyway. My customers make that very clear. In C I can see every alloc very clear. I fear by using c++ libraries I get some new or mallocs hidden in layers of code. I have not found a list of stl classen safe for embedded. That would be nice
- uecker 1y agoWhere do you see the advantage of using C++ then? What STL class would be useful without dynamic allocation? What I missed from C++ was vector, but this requires allocation and one can now do this in C too. For array handling, you can get better bounds checking in C and one can also define a decent span type.
- fuhsnn 1y agoThere are alternative STL-like libraries designed for embedded use: https://github.com/ETLCPP/etl https://github.com/ETLCPP/etl
- Surac 1y agoi see the benefit of c++ in the following points. Using Classes to hide away details, References as more safe pointers, Constructors/Destructors for stack based objects, more strict const handling, function overloading, operator overloading, hiding default or copy constructors, getter and setter with active code behind, namespaces, static classes to group helper functions, unifined init syntax over all datatypes, more strict type conversion rules, Templates, enum as real types
- beardyw 1y agoIt depends on how much headroom you have in memory, but C is the safe option. When you have 64 bytes of working (non program) memory, you pretty much need to declare everything up front anyway, so no stack either. It all depends.
- daviddever23box 1y agoIsn't this the premise upon which Google's Pigweed (https://pigweed.dev/ https://pigweed.dev/) operates?