Wednesday, May 24, 2006

Turning on optimization gives better warnings?

This could be one of those things that everyone knows except for me, but I learned a very hard lesson today. We had a code review internally for a bit of code I've been doing. A very embarrassing bug turned up that was the result of a poorly tested last minute query-replace.

The crazy thing was that it *should* have been caught by the compiler as an uninitialized variable. I had -Wall in my CFLAGS. Sure enough though, no matter how blatant I made the uninitialized usage, the compiler said nothing.

I looked in the info pages and discovered that uninitialized values are only reported if -O is present. -O does the necessary bookkeeping to allow data flow analysis which is clearly required for reporting of uninitialized values. I typically don't enable -O but I guess most projects I work on do. I will certainly be using -O from now on though.