Path: utzoo!attcan!uunet!ncrlnk!ncr-sd!hp-sdd!hplabs!decwrl!labrea!rutgers!ukma!husc6!yale!spolsky-joel From: spolsky-joel@CS.YALE.EDU (Joel Spolsky) Newsgroups: comp.sys.ibm.pc Subject: Re: Turbo C 2.0 weirdities Summary: Optimizing to blame Message-ID: <45380@yale-celray.yale.UUCP> Date: 11 Dec 88 22:33:39 GMT References: <1924@sun.soe.clarkson.edu> Sender: root@yale.UUCP Reply-To: spolsky-joel@CS.YALE.EDU (Joel Spolsky) Organization: Yale University Computer Science Dept, New Haven CT 06520-2158 Lines: 45 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