spam@clutx.clarkson.edu (Spam,,,) (12/12/88)
While debugging what appeared to be a perfectly legit piece of code, I noticed that a) some variables were ignoring commands that changed their values, and b) that some variables changed values in commands that did not reference them at all! More specifically: 1) While calling one function, a variable utterly ignored a command that should have set it to zero. I was watching the variable in trace mode, and it kept its value. 2) While executing a sscanf, another variable changed that wasn't referenced at all. The sscanf worked fine on the variables it did reference. I was parsing an input line of hex numbers that was terminated by a '$'. I then did a sscanf(line,"%X %[^$]",&token, line) which chopped off the first hex number and assigned the rest to itself (w/o the $). I see no reason why this should change another variable to -1. Any suggestions? _______________________ Roger Gonzalez Clarkson University spam@clutx.clarkson.edu
spolsky-joel@CS.YALE.EDU (Joel Spolsky) (12/12/88)
In article <1924@sun.soe.clarkson.edu> spam@clutx.clarkson.edu (Spam,,,) writes: >While debugging what appeared to be a perfectly legit >piece of code, I noticed that a) some variables were >ignoring commands that changed their values, and b) that >some variables changed values in commands that did not >reference them at all! The Turbo-C optimizer often manipulates your code so that it doesn't resemble the source. In a program like: main() { int c; c=0; printf("Hello\n"); } ...the statement c=0; won't even be compiled since it cannot possibly have any effect, well, at least, that's what the optimizer thinks. So when you execute it in the debugger, lo and behold, the value of 'c' (which probably wasn't allocated anyway) doesn't change. Or, this code: for (;;) { c=' '; putchar(c); } will in all likelihood be compiled as c=' '; for (;;) { putchar(c); } or worse. Thus, things don't happen exactly as expected. Very often TC is nice enough to give you warnings like "Unreachable code" or "variable declared and never used" etc etc when it is doing these things, but not always. +----------------+----------------------------------------------------------+ | Joel Spolsky | bitnet: spolsky@yalecs.bitnet uucp: ...!yale!spolsky | | | internet: spolsky@cs.yale.edu voicenet: 203-436-1483 | +----------------+----------------------------------------------------------+ #include <disclaimer.h>