2006.Jul.9
Filed under: Programming — jon @ 12:13

An interesting quote from (the very quotable) The Design and Evolution of C++ by Bjarne Stroustrup (emphasis added):

My impression was and is that many programming languages and tools represent solutions looking for problems, and I was determined that my work should not fall into that category. Thus, I follow the literature on programming languages and the debates about programming languages primarily looking for ideas for solutions to problems my colleagues and I have encountered in real applications. Other programming languages constitute a mountain of ideas and inspiration — but it has to be mined carefully to avoid featurism and inconsistencies. The main sources for ideas for C++ were Simula, Algol68, and later Clu, Ada, and ML. The key to good design is insight into problems, not the provision of the most advanced features.

Also, a C bug-hunt trivia question. Can you spot the logic error in this C code I recently saw?

int i;
int bool = 1;    /* assume success unless function returns 0 */
for (i=0; i < numdevices; ++i)
    bool &= function_returning_number_of_records_processed(i);
if (!bool)
    error("blah blah");

Hint: in the simplest case, the number of records processed can be either 0 (an error), 1, or 2.

Stab, stab, stab to the programmer. The answer appears in the comments.

1 Comment »

  1. The short answer: the programmer was trying to do a C++-ish thing (use a boolean-value variable) but botched it: int is not bool.

    The long answer. The &= operation is a bitwise or, not a logical (boolean) one. If the function returned the value 2 (or any even value), bool would be set to 2 & 1, which is 0, erroneously indicating an error when there was none. I believe the original programmer was thinking of 2 && 1, which would of course work correctly. Alas, there is no &&= operator in C or C++.

    Comment by jon — 2006.Jul.9 @ 12:47

RSS feed for comments on this post. TrackBack URI

Leave a comment

  • dreamhost.com logo
    Happily hosted by dreamhost.com
  • This site is green.
  • Bike Month
    miles biked47.50
    gallons saved1.36
    dollars saved$5.22
    pounds CO2 saved26.60
    calories burned1677