Dear Sirs,

I use gcc 3.4.5 and received following incorrect result in the program.
On the gcc site exist report about incorrect behavior of ++ operator but not 
this situation. I hope that it will be usefull.

I have developed some stupid code to show the problem:

#include <stdio.h>

void upcase(char* str) {
    size_t len;
    
    len = strlen(str);
    while(len-- > 0 )
        *str++ = toupper(*str);
        
}

int main(int argc, char** argv) {
  char buf[1000];
                
  scanf("%s",&buf);     
  upcase(buf);
  printf("\n%s",buf);

}


Compile: gcc -o test.exe -O2 tcode.c


Problem:
Without optimization such chunk of code is working fine, but with O2 
optimization,
I have incorrect result due to gcc incremented esi earlier than read data and 
as 
the result - the first letter is lost.

-------- DISASM -------------
 mov     ebx, esi
 inc     esi
 movsx   eax, byte ptr [esi]
 mov     [esp+18h+var_18], eax
 call    toupper
 mov     [ebx], al
-------- DISASM -------------

Best Regards,
crow16384


Reply via email to