Today at work, I ran into the first bug in my programming career caused by a bug in the compiler. The compiler in question: Borland C++ Builder 5. Anticlimactically, I figured it out rather quickly; Borland’s IDE has a nice “pop up a commented disassembly when things go really wrong” feature, and a quick glance at it revealed the culprit.

What I was doing was this: I had created an array of (lightweight) objects on the stack, which I was attempting to initialize inline. I was using a previously initialized object to do this, calling one of its overloaded operators. Basically, it looked something like this:

TDateTime array[] = { date - 5, date - 3, date - 1, date };

The generated code, when filling in the array, initialized the first element correctly. But subsequent calls to TDateTime::operator- weren’t right at all; dereferencing a some memory that it thought was a function pointer, but which was actually 4 random bytes of code. If it had gotten far enough without crashing, it would even have called a method in another class, which would have caused all of the logs stored on the hardware module to be erased!

So, uh, for those of you using outdated versions of C++ Builder, keep that in mind. Me, I just thought it was neat to be able to legitimately blame the compiler for something.

Leave a Reply