It's not every day you get to read an advanced book on C programming - there simply aren't many of them on the market. And finding a good one is harder yet.

David R. Hanson is the author of LCC - a retargetable C compiler, and of a book about it. In "C Interfaces and Implementations" (let's abridge it to CII from now on) he sets to explain some of the lessons he learned while implementing a large body of C code. Whoever does this, almost always runs into similar issues of implementing generic C modules for reuse, since C by itself is low-level, its standard library is very limited and basic, and there aren't many common C libraries around which are universally used (especially in the beginning of the 1990s, when the book was being written).

CII demonstrates more than 20 interfaces - with complete source code for both the interface and the implementation, from low-level to relatively high level. After presenting the basic cornerstones of memory management and exception handling (implemented as a set of macros on top of setjmp/longjmp), the book goes on to data structures (lists, hash tables, and so on), string formatting and manipulation, threads and finally arbitrary-precision integer arithmetic.

The presentation style is unique - the whole book is a living example of literate programming - apparently you can just use a LP tool to pull the code from the book automatically into a compilable form. This has some advantages like keeping the code in a runnable form and interspersing code samples with text - which is documentation. On the other hand, the LP style is sometimes taken too far. I really hated it when several lines from a function were pulled out to another section. If this book is for advanced C programmers, the author should've realized that those like to see the whole code for a function in front of the eyes (as long as it's not too long). Jumping from section to section for that is sub-optimal.

Another minor criticism is the code style. Admittedly, every programmer has his preferred style and "alien style" usually isn't appreciated. But this book has some really strange style choices, such a defining T to be the main interface type in all the interfaces. Sure it saves typing, but it has many obvious cons and I didn't like it. The author also provides some bad advice about magic constants and in other minor matters.

Overall, however, I must admit I really enjoyed reading the book. The principles behind it are strong and sound, and the interfaces it presents are mostly good examples of what you can achieve with reasonable amount of effort in C. We've all implemented similar interfaces on our own, and since just reading someone else's undocumented code is an activity few will pursue in their leisure time, I think that this is a "must" book - since it presents a lot of well thought-out and generally well-written code, profusely documented and explained.


Comments

comments powered by Disqus