Q263606: BUG: Optimization Bug When Using Array with memset Initializer
Article: Q263606
Product(s): Microsoft C Compiler
Version(s): winnt:6.0
Operating System(s):
Keyword(s): kbCodeGen kbCompiler kbVC600bug
Last Modified: 03-MAY-2001
-------------------------------------------------------------------------------
The information in this article applies to:
- The C/C++ Compiler (CL.EXE), included with:
- Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0
- Microsoft Visual C++, 32-bit Professional Edition, version 6.0
- Microsoft Visual C++, 32-bit Learning Edition, version 6.0
-------------------------------------------------------------------------------
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 this to be a bug in the Microsoft products listed at the
beginning of this article.
MORE INFORMATION
================
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.*/
#include <stdio.h>
#include <string.h>
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;
#ifndef WORKAROUND
A *pa= (A *)memset(&a, 0, sizeof(A));
#else
memset(&a, 0, sizeof(A));
A *pa= &a;
#endif
B bs[5];
memset_surrogate(bs, -1, sizeof(B));
printf("%d\n", pa->s[0]); // should print 0, prints -1
return 0;
}
Additional query words:
======================================================================
Keywords : kbCodeGen kbCompiler kbVC600bug
Technology : kbVCsearch kbAudDeveloper kbCVCComp
Version : winnt:6.0
Issue type : kbbug
=============================================================================
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.
Copyright Microsoft Corporation 1986-2002.