Date: Sun, 15 Jan 1995 11:50:03 +0900 From: Stephen Turnbull To: drupp AT cs DOT washington DOT edu Cc: djgpp AT sun DOT soe DOT clarkson DOT edu Subject: gcc = gcc -O2 ? From: drupp AT cs DOT washington DOT edu (Douglas Rupp) Marin Oldfield writes: "I think that changing the default optimization level as a side-effect of -g is dangerous and counterintuitive." ------- I don't understand your reasoning here. It seems to me that turning off all optimization when debugging (by default) is exactly what you would expect and want. The point is that we want flags to be orthogonal in their operation. It is true that at the moment it is relatively hard to (source-level) debug optimized code (temporary variables appear and disappear, references to named variables can disappear, etc), but this can fixed by better compiler and debugger technology. The problem is that optimization introduces changes in the code which can expose bugs or nonportable practices. For one example, literal strings should be considered read-only, because a certain amount of space can be saved by making the (hidden) pointers point to common string storage where tails of strings are the same. As far as I know, GNU C always does this optimization, but let's suppose that it only does it for optimization levels greater than 1. Consider a program which has a char* variable initialized by a string literal, and as a space optimization changes a couple of characters. (Eg, the string is "Press \"Y\" to continue: ", and depending on the context of the preceding text we might want the trigger key to be 'N'.) Then with the "-g => -O0" scheme, we compile with "-g", debug, and have no problems. So now we compile without "-g" to save the disk space, and if that string appears several times in a file, the compiler would optimize the storage. Those uses that do *not* change the string's value will get the wrong string. OK, we have a bug, so we turn on "-g" to debug, and the bug disappears. This is what Martin meant by "dangerous". Yes, if you read the manual carefully, you will know that you should turn on "-O0". The juxtaposition of "turn on" with the "optimization off" flag is intentional. This is what Martin meant by "counterintuitive." --Steve