FIX: Compiler Generates Bad Char Pointer Code with /Ot

Last reviewed: September 18, 1997
Article 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:

  • The Microsoft C/C++ Compiler (CL.EXE), included with:

        - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
        - Microsoft C for OS/2, versions 6.0 and 6.0a
        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, version 1.0
    

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:

  • In the sample code in the "MORE INFORMATION" section, below, remove the register modifier on the integer variable or remove the unsigned modifier on the character-string variable.

    -or-

  • Disable optimization by either compiling using the /Od option or by using #pragma optimize to disable optimization on a per-function basis.

    -or-

  • If you are using the C/C++ Compiler, version 7.0 or later, using the /f option (fast compile) causes the compiler to generate correct code.

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 : CodeGen kb16bitonly kbbuglist kbfixlist kbtool
Version : 6.00 6.00a 6.00ax 7.00 | 6.00 6.
Platform : MS-DOS OS/2 WINDOWS
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.