C++ in embedded

Hi all

If I am not wrong the C++ is barely used in embedded
programming , C is the facto language.

So I want to ask : what is the real advantage of C++ classes
which is the main difference to C ?

I ask in relation to embedded programming NOT
desktop programming .

Thanks
Elico

If I am not wrong the C++ is barely used in embedded programming ,

I'd say you are wrong.
You probably have a mobile phone.
That'll almost certainly have a lot of C++ in it.

The Symbian mobile OS is almost exclusively C++, except for very low-level functions.

Although most people write in C on the Arduino in fact lot of the Arduino code is C++.

Used properly C++ is very appropriate for embedded work.


Rob

"So I want to ask : what is the real advantage of C++ classes
which is the main difference to C ?"

Elico

Code reuse.
Better abstraction mechanisms.

AWOL:
Code reuse.

To make this very concrete.
Consider the blink without delay example
If this code was written as a class (so C++) you could easily extend the program to blink a led on each and every pin.
With the current code this is not so easy.

AWOL:
Better abstraction mechanisms.

To make this concrete:
I have a big project with plenty of classes. For debugging purposes I log the state of all classes at every loop run.
To do so all classes have the same method called void LogRow(stream str);
There are 3 levels of abstraction here.
The most obvious is that it does not matter what the class is doing (gps logger; motor driver; sensor) you just call classname.LogRow(); and you get your data logged.
The second level of abstraction is in stream. stream can be a sdcard file or it can be the serial monitor or it can be any device connected to a serial pin.
It does not matter. By changing 1 parameter you can have the logging on the sd card (for instance when the robot is outdoors running) or to your serial monitor (when you are trying to understand what is ongoing) or to a other device (Arduino/pi) which does something smart.
The third level is that a class can be derived from another class. In this case the LogRow() will logg its own data and then call LogRow() of the parent class.

I hope this makes things a bit clearer.
Best regards
Jantje

CLASSES vs funtctions .

Why this is easier/better then just using regular functions
that use parameters given to them when called
and acting accordingly ?
like func1(p1,p2,p3) ;

Elico

Because functions are not logically grouped and managed as they are in a class.
Functions cannot easily be instantiated for a variety of related objects, as classes can.
There is no inheritance for vanilla functions.

If you need to learn OOP, then a visit to a book store is better than asking random questions here.

Thanks

I did not asked you what to do ..

Elico

elico:
Thanks

I did not asked you what to do ..

Elico

Perhaps not, but when entire books are written primarily to answer the questions you ask here, then the answer to your questions are, go read the books.

For me as more hardware oriented person, I USE C++ classes only when I use such library code as the Arduino core functions have implemented. They are not hard to use. As a writer of Arduino sketches I try to ignore as many C++ features as I can get away with. I just write C functions, and sometime if I have a lot of related functions, say to interface with an external A/D converter for example, I will place the related functions into a 'tabbed' arduino edit window and include it in the main sketch, that way I can easily 'lift' them (copy and paste) into other sketches if I need them. Works for me. I'm kind of aware of the advantages of classes and their ability to have multiple instances but my brain is to small to figure out how and when to take advantage of such features and power. So straight C works for me and the Arduino platform allows me to swim in the same lake with all the bigger C++ guru fish. :smiley:

retrolefty:
So straight C works for me and the Arduino platform allows me to swim in the same lake with all the bigger C++ guru fish. :smiley:

It is a big lake. Feel welcome :smiley:
Best regards
Jantje

Jantje:

retrolefty:
So straight C works for me and the Arduino platform allows me to swim in the same lake with all the bigger C++ guru fish. :smiley:

It is a big lake. Feel welcome :smiley:
Best regards
Jantje

Thanks, I do feel I belong and I'm pretty safe here as most of the C++ fishes around these parts are not predatory, but I do have to keep a careful eye out for Coding Badly as he can be unpredictable at times. :smiley:

Graynomad:
Used properly C++ is very appropriate for embedded work.

And when used improperly C++ is very inappropriate for embedded work.

Actually, I think the OP has an important and valid point regarding the nature of C vs C++.

C is "small" langauge. Compact, purpose designed to build an OS (Unix, of course.) There was only enough added to the language to make it do what was needed, no more. It was designed to keep the programmer "close to the metal".

C++ is a "large" language. It was developed because for very large projects, C lacked some features that made maintaining and re-using code in large code bases more manageable. In C++, the "metal" can seem a long way away.

But there's the problem. A lot of C++ has to be avoided, or treated very carefully, when doing embedded work. As a large language, with lots of (often redundant) features (redundant in the sense that it gives you several ways of accomplishing the same thing), if you aren't careful you can easily get code bloat with lots of stuff being wheelbarrowed in under the hood, so to speak.

So what is an embedded programmer to do? Simple: you ruthlessly "subset" C++, to just the bits you need, and everything will still be fairly efficient.

And guess what? That subset looks pretty close to original C! Funny, that.

Now, there is a bit of "syntactic sugar" you get when using C++ over C, even in the heavily subsetted form. You can use methods and classes rather than structures and functions, which are tied together a bit more closely, and perhaps more elegantly. But do you really need all that additional data hiding and abstraction in an embedded project? Probably not. A well-written and structured pure C program for an embedded project will probably be just as maintainable and manageable as its well-written C++ counterpart. The code base is never going to get so large, and the programming teams maintaining it so large that the language differences really kick in to affect productivity.

And, as a final word, watch out for that string class. :slight_smile:

Pico, if my vote counted for anything I would vote for your post as the definitive statement on all things OO, C++ and Arduino.

It really annoys me that everyone jumps to the defense of Arduino as an object orientated platform when the truth is if you can't make your own judgement as to whether its OO or not, your efforts to learn OO and Arduino at the same time will lead to endless frustration.

Anyway very well put Pico.

Duane B

rcarduino.blogspot.com

DuaneB:
your efforts to learn OO and Arduino at the same time will lead to endless frustration.

Actually, this raises another important practical issue for C vs C++ that crops up a lot here in these forums: Because C++ is such a large, and sometimes quite unwieldy language, the beginner can easily become completely overwhelmed and lost, not seeing the woods for the trees.

C, being a small and compact language, is much quicker to learn, and I would recommend anyone not familiar with either language to start by reading K&R, and only after a bit of success with plain old C, to move onto to the relatively vast terrains of C++.

And thanks for your vote! :slight_smile:

A well-written and structured pure C program for an embedded project will probably be just as maintainable and manageable as its well-written C++ counterpart.

I'm currently writing/porting an Arduino-style API to LPCs, because in LPC XPresso land the C compiler is free but the C++ compiler costs $256 I'm doing it entirely in C.

I think it's practical to write good OO-style code in C and I think that's what I'm doing, I would love some of the C++ features but that can't be helped in this case :frowning:


Rob

Pico , very well done inclusive answer .

Many thanks
I will stick to C , bare C for embedded .

Elico

elico:
If I am not wrong the C++ is barely used in embedded
programming , C is the facto language.

So I want to ask : what is the real advantage of C++ classes
which is the main difference to C ?

'C' is only used by diehards. There's no advantage to using a C compiler instead of a C++ compiler unless the C++ compiler for your platform is rubbish (which barely happens these days as they all use GCC).

From the language point of view, objects can usually organize your work much better.

Example: Imagine you have 5 RGB LEDs connected, each with its own color and pin mapping. Each of those is clearly an object and will map nicely to a C++ struct.

You can program it in C with structs but every function to manipulate them will need a pointer to the LED's struct. Guess what? This is exactly what C++ is doing anyway, but neater.

Embedded programming is probably going to use an intermediate level between C and C++. It isn't going to use the full facilities of C++ (like STL, namespaces, RAII, etc.). You'll probably have more global variables than a purist would like, etc. That's fair enough because the programs being written are small.

If you restrict yourself to pure C then you lose that ability to choose a level. Things like LED objects are C++ but they make perfect sense. Why wouldn't you allow yourself to use them?

Why wouldn't you allow yourself to use them?

We all do, but we have more than enough water under the bridge to decide when its a good idea or not. Someone picking up and Arduino and a learn C++ book at the same time is in a very different situation though -

why does my program crash when i use new ?

is Arduino C++ the string class kills my program ?

etc etc etc

Duane B