C9111001: Incorrect Code for Assignment of Packed Structure

ID Number: Q78621

6.00 6.00a 6.00ax | 6.00 6.00a

MS-DOS | OS/2

buglist6.00 buglist6.00a buglist6.00ax fixlist7.00

Summary:

PROBLEM ID: C9111001

SYMPTOMS

Under the following conditions, Microsoft C versions 6.0, 6.0a, and

6.0ax generate incorrect code for the statement:

struct2=struct1

1. Struct1 is packed on a 1-byte boundary with #pragma pack(1).

2. Struct2 is a local variable that is packed on either a 2-byte or

a 4-byte boundary with #pragma pack(2) or #pragma pack(4).

3. The first member of the structure is a character.

4. Compile options do not include /Zi, /Od, or /qc.

RESOLUTION

The problem can be resolved with any of the following workarounds:

1. Make sure that the first member of the structure is not a char.

For example, if the positions of arg1 and arg2 are switched in the

sample code, then the correct code will be generated.

-or-

2. Compile with /Zi, /Od, or /qc.

-or-

3. Pack struct1 on a different byte boundary.

-or-

4. Make struct2 volatile or global.

STATUS

Microsoft has confirmed this to be a problem in Microsoft C versions

6.0, 6.0a, and 6.0ax. This problem was corrected in C version 7.0.

More Information:

Sample Code

-----------

/* Compile options needed: none

*/

#include <stdio.h>

#pragma pack(1)

struct MB

{

char arg1;

int arg2;

char arg3;

};

struct MB struct1;

#pragma pack(2)

void main( )

{

struct MB struct2;

struct1.arg2 = 8;

struct2 = struct1;

if ( struct2.arg2 == 8 )

printf( "Yes, struct2.arg2 is correct.\n" );

else printf( "No, struct2.arg2 is not correct.\n" );

}

The bad code generated for struct2 = struct1 is

mov ax, WORD PTR _struct1

mov dx, WORD PTR _struct1+2

whereas it should be

mov ax, WORD PTR _struct1

mov dx, WORD PTR _struct1+2

mov WORD PTR _struct2, ax

mov WORD PTR _struct2+2, dx

Additional reference words: 6.00 6.00a 6.00ax