With respect to the headers containing the pragma, that’s only true in
newer headers. The original poster may be using the header out of a WDK
or DDK that had tape samples (like 3790.1830) but doesn’t have a pragma
in minitape.h.
With respect to the bit field packing, I tested this before I replied
with a quick console app. It does change the packing (at least with VS
2008).
typedef struct _MODE_DATA_COMPRESSION_PAGE_P {
UCHAR PageCode : 6;
UCHAR Reserved1 : 2;
UCHAR PageLength;
UCHAR Reserved2 : 6;
UCHAR DCC : 1;
UCHAR DCE : 1;
UCHAR Reserved3 : 5;
UCHAR RED : 2;
UCHAR DDE : 1;
UCHAR CompressionAlgorithm[4];
UCHAR DecompressionAlgorithm[4];
UCHAR Reserved4[4];
} MODE_DATA_COMPRESSION_PAGE_P, *PMODE_DATA_COMPRESSION_PAGE_P;
typedef struct _MODE_DATA_COMPRESSION_PAGE_I {
UINT PageCode : 6;
UINT Reserved1 : 2;
UCHAR PageLength;
UINT Reserved2 : 6;
UINT DCC : 1;
UINT DCE : 1;
UINT Reserved3 : 5;
UINT RED : 2;
UINT DDE : 1;
UCHAR CompressionAlgorithm[4];
UCHAR DecompressionAlgorithm[4];
UCHAR Reserved4[4];
} MODE_DATA_COMPRESSION_PAGE_I, *PMODE_DATA_COMPRESSION_PAGE_I;
int _tmain(int argc, _TCHAR* argv)
{
MODE_DATA_COMPRESSION_PAGE_P p = {0};
MODE_DATA_COMPRESSION_PAGE_I i = {0};
p.Reserved2 = 1;
i.Reserved2 = 1;
printf( “Header struct\n”);
DumpBuffer(&p, sizeof(p));
printf( “\nTim’s struct\n” );
DumpBuffer(&i, sizeof(i));
return 0;
}
Header struct
00000000 00 00 01 00 00 00 00 00 - 00 00 00 00 00 00 00 00
Tim’s struct
00000000 00 00 00 00 00 01 00 00 - 00 00 00 00 00 00 00 00
00000010 00 00 00 00 00
On 8/28/2014 10:41 AM, Tim Roberts wrote:
Jeff Glass wrote:
> While changing the UCHAR’s to UINTs eliminates the compiler warning.
> This is a SCSI structure (from a MS WDK header). It needs to be packed
> properly. Don’s suggestion is the way to go.
The Microsoft headers that include this already have the pragma to
disable 4214.
However, changing from UCHAR to UINT for the bitfields will not change
the packing in any way. Some people have developed a rule in their
brains that the type of a bitfield should be the smallest type that fits
the field size. That’s a false rule. The C and C++ standards have both
always mandated that the type of a bitfield should be either “int” or
“unsigned int” (and Microsoft’s compiler happens to have an extension
that allows “__int64” and “unsigned __int64” for fields larger than 32
bits).
There is no advantage to using “unsigned char” or “unsigned short” over
“unsigned int” in a bitfield, and using “unsigned int” makes you
standard-compliant.