BUG: Fast Compiler Generates Bad Code for Decrement Operator

Last reviewed: July 22, 1997
Article ID: Q116168
1.00 1.50 WINDOWS kbtool kbbuglist

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE), included with: Microsoft Visual C++ for Windows, versions 1.0 and 1.5

SYMPTOMS

The fast compiler may generate bad code for statements that use the decrement operator and may assign the decremented value back to the variable being decremented.

RESOLUTION

This problem can be worked around either by compiling using the optimizing compiler or by restructuring the code. Use of the optimizing compiler can be forced by using the /f- compiler switch. The sample code in the "MORE INFORMATION" section below can be used both to illustrate the problem and to show how the code can be restructured to work around the problem.

STATUS

Microsoft has confirmed this to be a bug in the C/C++ compiler for MS-DOS, versions 8.0 and 8.0c. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

This is not a problem with the 32-bit C/C++ compiler, version 8.0.

MORE INFORMATION

The following sample code can be used to demonstrate the problem.

Sample Code

/* Compile options needed: /f
*/
   #include <iostream.h>

   enum color { red, green, blue };

   void main()
   {
       color ColorEntered;
       int choice, first = 3, last = 4;

       do
       {
           cout << "\nEnter a 0 or 1. Enter 2 to end: ";
           cin >> choice;
           ColorEntered = (color)choice;

           switch(ColorEntered)
           {
               case red:
                   cout << "RED" << endl;
                   break;

               case green:
                   cout << "GREEN" << endl;

                   // The fast compiler generates bad code for this
                   // statment:
                   first = ( (first > 0) ? (first--) : (last) );

                   // To work around the problem restructure the code
                   // as follows:
                   /*
                   if( first > 0 )
                       first--;
                   else
                       first = last;
                   */

                   cout << "value of first: " << first << endl;
               break;

               case blue:
                   cout << "BLUE" << endl;
                   cout << "Ending" << endl;
                   break;

               default:
                   cout << "Value out of range, ending" << endl;
                   ColorEntered = blue;
                   break;
           }

       } while(ColorEntered != blue);
   }


Additional reference words: 1.00 1.50 8.00 8.00c
KBCategory: kbtool kbbuglist
KBSubcategory: CodeGen
Keywords : kb16bitonly


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: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.