Bug in C++ compilation for structures compiled with pragma pack (1) - by Oded5119

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
0
Sign in
to vote
ID 629550 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 12/7/2010 12:48:58 PM
Access Restriction Public

Description

While compiling a C++ windows console application were the Enhanced Instruction Set is enabled to SSE2, we are facing a problem compiling the following code:

#include <math.h>

#pragma pack(1)

  struct S
  {
    float a;
    float b;
  };
  struct S1 : public S
  {
    float c;
  };


#pragma pack()

int _tmain(int argc, _TCHAR* argv[])
{
  S1 *m_pDet = new S1;
  m_pDet->a = m_pDet->b = sqrt(128.0);

  printf("%f %f\n", m_pDet->a,m_pDet->b);
  return 0;
}

While running the code, m_pDet->b does not receive the correct value and memory is corrupted in that area. When looking at the disassembly code it seems that the instruction used is MOVUPS. This is due to the pragma pack (1) which if is disabled, then the compiler will use the MOVSS instruction instead which works. 

The problem is that MOVUPS is an incorrect instruction. It copies 128bit and not 32bit into the memory.
Sign in to post a comment.
Posted by Oded5119 on 12/14/2010 at 3:35 AM
I have added a zip file containing the project the produces the problem. It happens many times and on different computers.

Please note that it is important to have the /arch:SSE2 switch enabled for this to happen
Posted by Microsoft on 12/7/2010 at 6:05 PM
Thanks for your feedback.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 12/7/2010 at 1:25 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)