object-oriented c coding
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:
- Incomplete types
- Single inheritance in C programming
- Aggregation
- Naming conventions (I)
- Naming conventions (II)
- Symmetry
- Memory management
- Name mangling
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:
- The end result is a small-to-medium sized library component.
- highly-portable, cross-platform, generic code is a design requirement.
- Few external libraries are required, and of those, what’s needed from them can be statically linked.
- 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.









{ 2 comments… read them below or add one }
Fact is that a skilled C programmer can write BETTER OO than a skilled C++ programmer.
Haha! Not sure I would go that far… depends on who’s doing the coding. But it’s certainly possible to write snappy ) code in c.
Same with Win32, not that damn hard to write bullet proof code, for anyone who gives a crap about their code.
{ 1 trackback }