FIX: Compiler Generates Bad Char Pointer Code with /Ot

ID: Q117203

6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00

MS-DOS                 | OS/2       | WINDOWS
kbtool kbfixlist kbbuglist

The information in this article applies to:

SYMPTOMS

The compiler may treat an unsigned character pointer as an integer pointer when compiling using the /Ot option.

RESOLUTION

There are several ways to work around this problem:

STATUS

Microsoft has confirmed this to be a problem with the C Compiler for MS- DOS, versions 6.0, 6.0a, 6.0ax, 7.0, and 8.0. This problem was corrected in the C/C++ compiler for MS-DOS, version 8.0c, included with Visual C++ for MS-DOS, version 1.5.

This problem does not occur with the 32-bit C/C++ Compiler, version 8.0, included with Visual C++ 32-bit Edition, version 1.0.

MORE INFORMATION

Using the /Fc option, you can generate an assembly listing of the source code. This is an excerpt from the assembly listing of the sample code below, which shows where the error occurs:

   ;|***    while(ch==*str);
   ; Line 8
                $FC180:
    *** 00000f  8b 5e fe        mov   bx,WORD PTR [bp-2]      ;str
    *** 000012  8b 37           mov   si,WORD PTR [bx]
    *** 000014  81 e6 ff 00     and   si,255  ;00ffH
    *** 000018  75 f5           jne   $FC180

Notice the code uses WORD PTR, when it should be using BYTE PTR. The following is the code generated when one of the workarounds is used:

   ;    while(ch==*str);
   ; Line 8
                L00180:
    *** 000010  8b 5e fc         mov     bx,WORD PTR -4[bp]
    *** 000013  8a 07            mov     al,BYTE PTR [bx]
    *** 000015  25 ff 00         and     ax,OFFSET 255
    *** 000018  8b f0            mov     si,ax
    *** 00001a  8b c6            mov     ax,si
    *** 00001c  3d 00 00         cmp     ax,OFFSET 0
    *** 00001f  75 03 e9 00 00   je      L00181
    *** 000024  e9 e9 ff         jmp     L00180

Sample Code

/* Compile options needed: /Ot (/Fc to generate assembly listing)
*/ 

   #include<stdio.h>

   void main(void)
   {
      register int ch;
      unsigned char *str="abc";

      while(ch==*str);
   }

Additional reference words: 6.00 6.00a 6.00ax 7.00 8.00 1.00 KBCategory: kbtool kbfixlist kbbuglist KBSubcategory: CodeGen
Keywords          : kb16bitonly kbCodeGen kbbuglist kbfixlist
Version           : 6.00 6.00a 6.00ax 7.00 | 6.00 6.
Platform          : MS-DOS OS/2 WINDOWS
Solution Type     : kbfix

Last Reviewed: September 22, 1997