Microsoft KB Archive/263606

= BUG: Optimization Bug When Using Array with memset Initializer =

Article ID: 263606

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q263606



SYMPTOMS
The C/C++ compiler may generate incorrect code when compiled with the /O2 option if an array pointer is initialized with a call to the memset initializer.



RESOLUTION
Split the array initialization and pointer initialization into two steps as shown in the sample code below.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior
The following sample code demonstrates the bug and the workaround: //test.cpp // compiler option needed: /O2 OR  /Og /Oi /Ot /Oy

//#define WORKAROUND /* Uncomment this line for workaround.*/
 * 1) include 
 * 2) include 

typedef struct {signed char s[118];} A; typedef struct {signed char s[12];} B;

void *memset_surrogate(void *p, int i, size_t s) { return memset(p, i, s); }

int main { A a;  A *pa= (A *)memset(&a, 0, sizeof(A)); memset(&a, 0, sizeof(A)); A *pa= &a;
 * 1) ifndef WORKAROUND
 * 1) else
 * 1) endif

B bs[5]; memset_surrogate(bs, -1, sizeof(B)); printf(&quot;%d\n&quot;, pa->s[0]); // should print 0, prints -1 return 0; }

Keywords: kbbug kbcodegen kbcompiler KB263606

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.