Global Sources
EE Times-Asia
Stay in touch with EE Times Asia
?
EE Times-Asia > FPGAs/PLDs
?
?
FPGAs/PLDs??

Writing good C++ code for embedded applications

Posted: 16 May 2006 ?? ?Print Version ?Bookmark and Share

Keywords:colin walls? mentor graphics? mentor? spotlight? embedded?

Years ago, embedded developers were slow to pick up on high-level languages, but C eventually became accepted. It took the arrival of very high-quality code generation and transparent debugging for the industry to accept this change, and only after it came under relentless pressure to become more productive.

Even though the obvious step ahead from C is to C++, its use for embedded software is far from universal.

All this hesitation isn't due to pigheaded resistance to change. It's simply a matter of continued worries about resources. Memory may be bigger, but you can't just add an extra half a gigabyte when you need it. Processors are much faster, but cost and power limitations mean that they are way behind the monsters found in even modest PCs.

Is this concern about the suitability of C++ for embedded software justified? Not really. Like any tool, the language needs to be handled correctly to yield good results. Start by applying C++ incrementally. If you are using C today, it may be a big jump to using C++ tomorrow.

Do>
Understand how C++ actually works. Look at the produced code and try to understand why it's done the way it is.
Encapsulate expertise in objects. Embedded-software team members have diverse sets of skills. Encapsulate that expertise into classes so that it can be used safely by other team members.
Use tools that are optimized for embedded
. Although many companies offer tools for embedded programming, some tools are much more oriented toward the needs of embedded developers than others are.
Apply language features appropriately. C++ was not designed specifically for embedded applications. Some language features such as overloaded functions carry absolutely no cost in resources. Other features such as the exception-handling system (EHS) can cause significant overhead. This facility enables the programmer to construct very robust code. The downside is that a lot of code has been silently generated to accommodate this functionality. If it cannot be avoided, at least using the EHS in a simple way will minimize surprises.

Don't
Treat an embedded system like a PC. If you are programming a PC, you can assume that memory is infinite and free, and that you will always have enough CPU power. With embedded software, you need to be more circumspect. Write some code. Measure its size and its execution performance. Then, only when you are satisfied that these measurements are within reasonable bounds, move on.
Use purely top-down implementations. It may be attractive to build a software application by starting at the high level, just including stubs for lower-level functions. However, the downside of this strategy is the discovery of a nasty surprise. You can finish coding, and then find that the whole program is too big or too slow, or both. A better approach is to start at the other end.
Use deeply nested inheritance. Part of the point behind object-oriented programming is the ability to define classes in terms of other base classes that have been previously created by you or another developer without the need to fully understand the inner workings of the base classes. This is inheritance. The downside is that objects instantiated from such derived classes may carry some overhead.
Write "clever" code. C++ gives you the opportunity to write very clear, concise code. It also provides the means to write very convoluted, arcane code. Don't give in to the latter.
Avoid language features just because your tools don't handle them well. For instance, if templates are badly implemented, they can cause an enormous code bloat. If your tools don't handle such constructs well, use tools that will enable you to program productively.

- Colin Walls
Technology Specialist, Accelerated Technology Division
mentor graphics Corp.





Article Comments - Writing good C++ code for embedded a...
Comments:??
*? You can enter [0] more charecters.
*Verify code:
?
?
Webinars

Seminars

Visit Asia Webinars to learn about the latest in technology and get practical design tips.

?
?
Back to Top