Bob van der Poel started programming when 4K and a cassette recorder were considered a hi-tech home system. He currently writes system software for 0S-9/68000 operating systems. You can reach him at P.O. Box 355, Porthill, ID, 83853 or electronically via CompuServe [76510,2203].
Every once in a while I purchase a computer book which is not only useful, but is also amusing and fun to read. Obfuscated C and Other Mysteries is one of those books. The book covers three areas: the results of seven Obfuscated C Contests, reprints of many of the author's previously published articles (some have been updated and expanded), and several new essays on real-world programming problems and solutions.
Why Obfuscate C?
Despite the title, Obfuscated C is not the major theme. Eight of the book's 40 chapters cover the 1984 to 1991 Obfuscated C Code Contests this comprises only 182 of 414 pages. Each year programmers submit programs which are deliberately convoluted; winning programs are so difficult to decipher that only the most devoted programming sleuth can fathom either their operation or purpose. Don Libes has gathered together all of the winning entries and presented them in this book. Not only is all of the code presented; Libes also "decodes" most of the programs either with detailed explanations or unobfuscated versions of the code.Some may think it is wrong, or at least questionable, to deliberately include confusing code in any book. However, as Libes notes in the preface, we are all faced from time to time with the daunting task of deciphering code (even our own code, at times). By examining the contest code and its explanation we can hone some of those deciphering skills. Besides, a lot of the code is humorous and worth a laugh well, some of us think it's funny.
Problems and Solutions
The rest of the book deals with a wide variety of C topics. To highlight a few:
Each topic in the book is presented as a programming problem and solution. Libes, a professional programmer, presents very pragmatic solutions. He emphasizes the importance of finding solutions which work. When commonly accepted solutions need improvement, he points this out and offers alternatives. For example, in the chapter "Faster than Malloc", Libes discusses why malloc is expensive in terms of speed and memory. He then presents an alternate memory allocator useful for allocating a large number of fixed-length memory blocks. This alternate allocator is excellent for creating symbol tables. In the chapter "Byte Ordering," he not only explains the origin of the terms "little-endian" and "big-endian;" he also presents a set of conversion functions.
- Implementing variable-size arrays
- Translation programs
- Implementing sets with bit operations
- Context-independent macros
- Various malloc topics
- Getopt for parsing command-line arguments
- Using longjmp
- Pointers to functions
- Linked lists
- Portability
- Threads
- Software timers
- Register variables and program effeciency
- Tcl, Tool Control Language
In addition to the main topics, this book contains a wealth of useful tricks and techniques. For example, in the chapter "x=x++" there is an algorithm to determine the number of bits in an integer and a technique to pass arrays by value.
The Code
Throughout the book Libes has presented clear, consistently formatted code which is a mixture of Standard and Classic C. He uses prototypes; however, by including a proto.h header, he makes it possible to compile the code with both Standard and Classic C. Knowing that his readers program on many different computer systems, sometimes with outdated compilers, Libes has attempted to use only the most portable features of C. Most of the code should compile on any complete C implementation. When code relies on non-standard features, Libes notes this fact and suggests appropriate workarounds.My copy of Obfuscated C included a code disk. This 1.2Mb 5 1/4" MS-DOS disk contains two files: an archive file containing all the source code and an MS-DOS executable zoo utility to decompress the archive. The archive expands to over 2 Mb. The disk contains most of the code that is listed in the book; however, it does not reproduce some of the smaller code examples. In addition to the source code listed in the book, the disk contains a number of miscellaneous files:
Readers without access to a complete UNIX system may be frustrated by some of the documentation files, since they can only be printed with full-blown troff formatters. It would have been nice if Libes had included pre-formatted documentation files on the disk. Also, a 1.44 3 1/2" diskette would be useful for readers without a 1.2 Mb drive. Permission is granted in the book to use the code on the disk in your own programs; however, he requests that you acknowledge his authorship.
- A simulation program written by the author to study the "Entropy of Self-Avoiding Surfaces" using Oct-tree Data structures.
- Snake: yet another snake game.
- Steve Summit's list of "Frequently Asked Questions" from Usenet.
- Stan Brown's list of "Identifiers to Avoid;" documents all Standard C identifiers.
Layout, Organization, and Style
The book layout and organization is logical and concise. The topics are presented in order of increasing complexity. Most chapters are self-contained and do not rely on previous chapters. The index appears to be complete and well thought out.Obfuscated C and Other Mysteries is not designed to be an introductory text, nor is it a reference book. It is a collection of interesting and useful code, with explanations, for intermediate to advanced programmers. It is also a nice book to browse through. Each casual reading illuminates more arcane tricks and useful techniques. Overall, it is well written, easy to read, and, in the bargain, entertaining, with many nuggets of humor sprinkled throughout. If you are serious about C, or would just like to learn more about it, this would be a good book for your library.
Title: Obfuscated C
and Other Mysteries
Author: Don Libes
Publisher: John Wiley and Sons, Inc.
605 Third Avenue
New York, NY
10158-0012
Price: $39.95 (includes disk)
Pages: 414
ISBN: 0-471-57805-3