Home > Articles > Programming > C/C++

Object-Oriented Overhead: C++ and Embedded Environments

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close Window

Linden deCarmo

Learn more…

Sorry, this author hasn't posted any blogs.

Core Java Media Framework

From the author of
Core Java Media Framework

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.

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 ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

David ChisnallCocoa Tip of the Day, 1/29/10
By David Chisnall on January 29, 2010 No Comments

Don't ignore old versions of OS X.

See All Related Blogs

Informit Network