Reasons to use C++ in Embedded Systems
In the past programs for embedded systems have been written in C. However I think with the increasing power that micro-controllers are delivering, C++ is becoming a more viable programming language.
It doesn’t need to be limited to 32bit ARM Cortex M’s though. The 8/16bit Atmels AVRs, Microchips PIC and TI’s MSP430 all have C++ compiler for them.
Why do we still use C?
In many cases, the C language has catered for the embedded developer just fine. It’s simple, easy to understand and doesn’t have many quirky bits. There’s also a lot of control over how you store variables – where they are stored and how.
If you’re currently a C only developer, have no fear, you’ll still be in demand.
Use the tool that is best suited for the job. No point abstracting everything to classes if the job can be elegantly done with a struct.
Why start using C++?
As engineer’s we’re naturally curious. C++ is basically the C language with a few added extras. Imagine how more employable you will be if you can say that you’ve had C++ experience in embedded systems?
Computer Science people are comfortable with it
In the companies I have worked for, people with computer science backgrounds have been employed in programming embedded systems. You’re asking what they’re doing in an embedded environment?
CS programmers bring to the game:
Good Code Organisation
Being brought up on object oriented languages such Java and C++ at varsity, people with a CS back ground are keen to have code organised and structured.
Embedded programmers tend to use lots of #defines everywhere and in some cases, not even bother and use “magic numbers”
Having CS people on board keep the code in order.
Higher Level Thinkers
Where embedded programmers are involves with each bit, madly masking this and that out, CS tend to think about the larger picture.
They come up with efficient algorithms and are good at thinking about the final application as a whole. However to do this, CS want a language which gives them the power to do that, this is where C++ can be advantageous over C.
With C++ comes namespaces. Having namespaces means you can use the same function name over and over again without getting it confused with others.
On the surface it basically looks like replacing the ‘_’ with ‘::’. If you use an IDE which does code indexing (Eclipse, VS2010 etc) then you’ll also be able to jump between references and calls to any old ‘writeByte’ and it will know which one you’re after.
Because the C++ compiler will mangle the function names, using namespaces comes with hardly any extra cost.
But beware, you can’t use all C++ features
After trawling through the web personally, I’d stay away from using:
Run time type information (RTTI)
Exceptions (try, throw, catch)
C++ Standard Template Library (STL)
Mutliple Inheritance – the virtual table needs to be larger
Templates – use these with caution.
As will most embedded development, stay away from using the heap or anything that needs runtime memory allocation. This means ‘new’ & ‘delete’, ‘malloc’ and ‘free’ shouldn’t be used. The reason being is that most embedded systems don’t have a memory manager unit.
Using C++ can speed up your development time if you can get more people working on it. C++ is a widely accepted and used language in the world of computing, so if it means using a language that they understand, then why stick to plain old C?
You can use the best parts of C++ which add little overhead and at the same time have access to constructs and features which make the code more organised.
If you are using C++ in your embedded development I’d like to hear about it!