If you have used g++ and gcc over the years, you know that sometimes you get the worst compiler error of all:

g++: Internal error: Segmentation Fault (program cc1plus)

While this message is very friendly and informative, it doesn’t help you get your code compiled.

So, what do you do when your code masterpiece, for whatever reason, crashes the compiler. This typically happens, not when you are writing code, but when you update to a newer version of the compiler and amid your efforts to port your code base to the new compiler, the compiler dies.

Your first reaction will probably be to ask your boss if you can go home for the day — see if your luck will be better tomorrow. This is a good strategy, even though it seldom produces a solution to the problem (even if it does mysteriously solve the problem, it’s a little unnerving) — either way, you might get a half day off. Everyone needs a break once in a while.

Your second reaction will be to pass the problem on to someone more senior or someone more junior who might have more experience or time to deal with this kind of problem. Another good strategy — in the meantime, you can go get some coffee.

Your third reaction will be to find the person who wrote that code and ask him what he was thinking. “Do you have to solve everything with template meta-programming? Here, fool, let me show you how to write a for loop.” Another good strategy, it feels good to yell at people, it releases stress. And, if they are actually using template meta-programming where they could just use a for loop, they deserve it.

Your fourth and final reaction will be to dig into the code and solve the problem. You know which file it is crashing on, but since the compiler crashed, it doesn’t report a line number, so all you know is that you have a 20,000 lines of code with something the compiler doesn’t like buried in it. Here is a technique I learned from my brother who was writing code before most of you were wearing digital watches.

Put your computer science skills to use and do some divide and conquer. First #if 0 out the last 10,000 lines (breaking at a logical boundary, of course), see if it compiles without crashing. If it compiles, the problem is in the last half of the file so move your #if 0 somewhere around line 15000. If the compiler crashes, the problem is in the first half, move your #if 0 somewhere around line 5000. Repeat this binary search until you narrow down to a single statement or small section of code that causes the compiler to crash.

Then all you have to do is figure out the best way to restructure that code so the compiler can handle it. This could involve some guesswork and you should talk to the guy who you yelled at back in reaction three to get his opinion. By the way, you should probably remember this moment of humility the next time you hit stage three with a bug like this.

Now that you have solved this problem, you might want to narrow it down further and report the compiler crash to the compiler vendor or the gcc-bugs mailing list. Oh, yeah, and tell your boss what a tough problem you just solved — he might just give you another half day off.