BUG: Incorrect Code from Intrinsic memset() Routine

Last reviewed: July 22, 1997
Article ID: Q106400
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 compiler generates incorrect code for the intrinsic form of memset() in certain cases.

CAUSE

The compiler generates incorrect code for memset() if all of the following conditions are met:

  1. You are using the /Oi option, /O2 option (which includes /Oi), or "#pragma intrinsic( memset )" to instruct the compiler to generate the intrinsic version of the function.

  2. You are using the /G2 or /G3 options.

  3. The character to set (second argument) in the memset() call is greater than 15.

  4. The number of characters (third argument) is a constant, and it is less than 9.

RESOLUTION

To avoid the problem, use a variable instead of a constant for the number of characters (third argument), or eliminate one of the above conditions. See the comments in the sample code below.

STATUS

Microsoft has confirmed this to be a bug in the products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

This problem does not occur with Visual C++ 32-bit Edition.

MORE INFORMATION

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

Sample Code

/* Compile options needed: </G2 or /G3> and </Oi or /O2>
*/

#include <stdio.h>
#include <string.h>

void main( void )
{
    char ach[11] ;
/*  int n = 8;  */  /* uncomment and use n below */

    ach[10] = '\0';

    memset( ach, 'A', 10 );
    memset( ach, 'B', 8 );  /* to avoid problem use n, not constant 8 */

    printf( ach );
    if ( ach[7] != 'B' )
        printf( "\nsecond memset() failed" );
    else
        printf( "\nsecond memset() succeeded" );
}


Additional reference words: 1.00 1.50 8.00 8.00c
KBCategory: kbtool kbbuglist
KBSubcategory: CLIss
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.