You are here: Home » object-oriented c coding

object-oriented c coding

by david

I write a fair bit of code in the C programming language. It turns out that it’s possible to write reasonably understandable object-oriented code (OO) in C, including a limited form of single inheritance. In the following article, I will outline the techniques I use. Topics include:

An interesting side effect of teaching myself how to accomplish these tasks in C is that I have a much better understanding of how OO programming really works, and what some of the issues are in designing an OO language, and writing the compiler.

When to code object oriented C

Over the last few years, C++ technology has matured immensely, even as the mindshare of the C++ language has diminished, followin the rise of web applications in favor of “traditional” desktop applications. So asking if and when to use object oriented C coding techniques in lieu of C++ is a relevant question.

In my opinion, object-oriented C remains competitive under the following conditions:

  1. The end result is a small-to-medium sized library component.
  2. highly-portable, cross-platform, generic code is a design requirement.
  3. Few external libraries are required, and of those, what’s needed from them can be statically linked.
  4. The memory footprint is small and doesn’t grow (quickly) over time during the application’s execution.

Disadvantages

The chief disadvantage of writing object-oriented C isn’t so much about C, it’s more about the last several years seeing C++ really mature into a standard, well-supported language, supported even by Microsoft! It took Microsoft a long time, but even they are finally shipping Visual Studio with standards-compliant STL. (This is a big deal.)

Other disadvantages include:

  • The developer really does need to understand OO design principles. C doesn’t attempt enforce OO design as Java and C++ do (and I have seen enough “C++-TRAN” and “JAVATRAN” to know how effective such enforcement really is.)
  • Solid C coding skills are required, especially with respect to memory handling. If you can’t write well in C, trying to write object-oriented C could be difficult. If not to you, then to anyone following along.
  • You have to do your own name mangling. C++ implements polymorphism by examining the argument list of a function, then creating a new function name at the linker level. Microsoft creates this new function name using an arcane system of symbols denoting the argument types. When you design object-oriented C code, you are in charge of name mangling, and using a rational and systematic scheme for naming functions is critical. (Check out Part II as well.)

There’s more!

Further topics not necessarily restricted to OO programming will include:

  • Pros and cons of modular code: sometimes, one single file makes a lot of sense.
Share and Enjoy:
  • Digg
  • StumbleUpon
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • TwitThis

{ 1 trackback }

There Is NO Box will be ramping down for a while | There Is NO Box
June 12, 2009 at 7:48 pm

{ 0 comments… add one now }

Leave a Comment