On Wed, Dec 31, 2003 at 07:51:23AM -0500, Mark Gray wrote:
Marcus Meissner <meissner@suse.de> writes:
while (*s) { *s++ = toupper(*s); }
loop. What exactly makes the old heretic code so wrong in the eyes of ISO C? A list of example problematic code would be most welcome. (A proper warning from gcc when using optimization would be nice as well.)
The problem here are so called 'sequence points'. ';' is such a sequence point.
Using s and s++ within the same sequence will result in undefined behaviour like you noticed.
while (*s) { ... do stuff ... s++; }
is fine.
Ahah -- that I can see now when spelled out (the programmer only mentioned a change, without any "legaleeze.")
But why is there no warning when optimization is turned on? (When the warning is actually needed :-) This is my main point.
It is there with -Wall on SuSE Linux 9.0: $ cat xx.c void f(char *s) { while (*s) { *s++ = *s | 0x20; } } $ gcc -O2 -Wall -c xx.c xx.c: In function `f': xx.c:3: Warnung: operation on `s' may be undefined $ Ciao, Marcus