Home > Articles > Programming > C/C++

Object-Oriented Overhead: C++ and Embedded Environments

  • Print
  • + Share This
C++ can give you the benefits of object-oriented programming in a restricted environment. If C++ isn't possible for your project, you can create C interfaces that mimic C++ class concepts.
From the author of

Most Windows and Linux programmers who migrate to embedded development assume that they should be able to use all aspects of C++ in their development projects. By contrast, old-school embedded programmers are emphatic that C++ bloats code and causes significant performance problems in time-critical embedded applications. To resolve this debate, we'll reveal the C++ features that are safe to use in embedded environments with limited memory and restricted processing resources.

Class Envy

Until the late 1990s, the C++ class keyword was strictly verboten for embedded development because the majority of compilers were ANSI C only. Furthermore, since 32-bit embedded processors were rare, processor cycles were precious and couldn't be burned on object-oriented overhead. By comparison, modern environments offer powerful 32-bit processors and the GNU Foundation's C++ compiler (gcc) has been ported virtually everywhere.

Since these restrictions have been lifted, many naïve engineers assume that they can freely use multiple inheritance, templates, and other advanced C++ features. Unfortunately, while such features will compile, they're likely to produce bloated code that can't meet real-time performance requirements.

Clearly, there is overhead in the creation and usage of a C++ class or struct compared to a C struct, due to the compiler's imposition of a constructor/destructor and the hidden use of the this parameter in class member functions. Fortunately, this penalty is slight on register-rich RISC processors; if you use inline constructors and destructors, the overhead is actually minimal.

While C++ typically requires more processing resources than C, when properly used its benefits exceed the additional overhead it imposes. For instance, the C++ class keyword offers data abstraction features that enable you to create well-defined interfaces that are less vulnerable than the equivalent C interface to incorrect usage and global namespace conflicts. This is crucial because stability is just as important as performance for embedded applications—for example, you don't want your TV rebooting every thirty minutes due to a stray pointer.

  • + Share This
  • 🔖 Save To Your Account