Is it recommended/practical to use C++ instead of plain C ?

Posts: 384
Joined: Sun May 14, 2017 5:36 pm

Re: Is it recommended/practical to use C++ instead of plain C ?

Postby permal » Thu Nov 14, 2019 5:56 pm

Late to the game, but the answer is absolutely "yes".

I've written C++ on the ESP32 since day one (2.5 years ago) and never regretted the decision to do so. As always, you have to know your tool, so if you know C++, then there's no reason not use it on this platform imho. With the xtensa-gcc 8.2 compiler there's now support for C++17 so you have access to the new modern tools too.

Now that we're on the topic, I have to drop this shameless self-plug here in case you're wondering how people have taken C++ (and abstractions) on this platform.

Posts: 583
Joined: Mon Jun 04, 2018 2:47 pm

Re: Is it recommended/practical to use C++ instead of plain C ?

Postby PeterR » Thu Nov 14, 2019 10:13 pm

Cool, I will check your link.
EDIT: 'Know your tool' - absolutely & following.
I draw short of 'absolutely' if only to challenge the C die hards and to present 'appropriate use'. C++ includes C so technically you are correct (excluding designated intitialisation which really anoys me when porting).
C++ - yes. The problem is that people with limited C++ experience (but maybe have a soul & are C++ curious) then (because they have seen inappropriate use) read in inappropriate use!
Its like me quoting Yes that's some bad C but I am not saying C is bad! Must review & name that formal falacy!

So you could use a vector and reserve or you can push and dynamically allocate. Err, did you want a dynamic array? If so you have to pay for that.
So the argument goes that C++ is bad because it might malloc if you do not tell it not too. Well, err you make that call, reserve ffs!

The fact is that dynamic allocation will only happen if you do not use the library appropriately and/or want that flexiabiliy.
The answer? If you do not understand that library's impact then revert to 'C'. Learn one small C++ part at a time.

I do feel for those (as earlier posted) who have been sold C++ as the silver arrow without the broader plays being put in place (e.g. training).
Its a tool, invest in training, increase productivity and get over it! EDIT: Or (sans a genuine buisiness UP, watch your (most capable) team leave over time).

I love C# and will burn C/C++ on the stake of 'reflection' once I have the ram etc for my embedded work (Moore's law/experience & ESP32 tells me that is not far off). My main drag (as I suspect for most) is time & that is an organisational/investment thing.
I will be in the car park should anyone wish to discuss further, just saying...
& I also believe that IDF CAN should be fixed.

Posts: 301
Joined: Tue Mar 28, 2017 8:03 pm

Re: Is it recommended/practical to use C++ instead of plain C ?

Postby jcsbanks » Fri Nov 15, 2019 9:52 am

I use std::map, std::string, std::vector, find and iterators. The error messages are not nice and some things need just as much care for memory management as malloc/free because you can easily run out of PSRAM, it comes down to design and algorithm, and appropriate use and I would not criticise C++ when it just needs to be used appropriately.

It might be the style of programming, but when trying to use code an experienced colleague wrote in C++, it was verbose, bloated and slow. Getters and setters had everything seemingly written three times and an attempt to use boost XML serialisation failed miserably. It was hard to debug and the object orientation was annoyingly obtuse. Rewriting using csv instead of xml and using std::string and vector had the code running faster on the ESP32 than it did on a PC with an order of magnitude less memory use. Again picking the appropriate design rather than C++ criticism, but inappropriate design and C++ can go together.

I'm influenced by firmware in modern engine control units in terms of their reliability, safety, operational life. The memory and processing power are similar to an ESP32 (with a lot more I/O) and there is usually about 2-3MB of compiled C code. No dynamic allocation at all, no C++ or any classes in sight. Parts of the std library have really helped for the dynamic (eg webpage, server, file systems, running scripts) stuff on the ESP32, doing those in C99 would not be too much fun.

Who is online

Users browsing this forum: No registered users and 62 guests