BUG: Incorrect Code from Intrinsic memset() Routine

ID: Q106400

1.00 1.50 WINDOWS kbtool kbbuglist

The information in this article applies to:

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

Last Reviewed: July 23, 1997