Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!hp-pcd!hpfcso!mjs From: mjs@hpfcso.FC.HP.COM (Marc Sabatella) Newsgroups: comp.sys.hp Subject: Re: cc optimizer dies on a few perl.4.0.beta files Message-ID: <7370358@hpfcso.FC.HP.COM> Date: 3 Apr 91 22:23:58 GMT References: Organization: Hewlett-Packard, Fort Collins, CO, USA Lines: 31 >As it happened, I left out the code >to initialize a variable before use. I never noticed, cause apparently, >without -O, the compiler decided to be nice and initialize it for me. Once >the program was "finished", and I added -O to the Makefile, it no longer >worked. After some playing, I finally figured out the error, and added the >code to initialize the variable, and compiling with -O worked just fine. >Apparently, the optimizer said "you want things optimized, and initializing >something for you takes extra cycles, so I won't do it - Nyah!" (insert >picture of compiler with its toungue sticking out :-). > >I never got a warning or error message in either case, so either I don't >have the warning message level set right, or the compiler just doesn't tell >you about these things I should remember to do in the first place. We'd love to take credit for adding the initialization code, and warn when we add it, but that is not what happened. The compiler added no such code. Normally, local variables not declared "static" or "register" are allocated on the stack. Often, those locations on the stack just happen to contain zeroes, particularly early in the execution of the program, before the stack has become cluttered. When optimized, however, such locals are put in registers, which "happen" to be zero a whole lot less often. So the compiler is not actually as smart as you imply, nor is it deliberately mocking your programming style or sticking its tongue out at you :-) BTW, lint can't do it all. A classic example of something it can't do: an unitialized character array, "initialized" via a "strncpy" that does not copy a terminating null character. Of course, this array is unlikely to be allocated to a register when optimized, but optimization may affect the stack contents, as can seemingly unrelated changes to other routines in your program.