LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Sizeof bit field struct unaffected by pragma pack

I'm trying to use a bit field as part of a larger structure to map out some data returned by another device.  I'm using the #pragma pack(1) command to ensure that no padding is added to the structure.  However, it appears that the bit field structure adds padding anyway.  Consider this exmaple:

 

    #pragma pack(push, 1)

    struct BIT_FIELD

    {

        unsigned int Bits: 8;

    };

    struct CHAR

    {

        unsigned char Char;

    };

    int x = sizeof(struct BIT_FIELD);

    int y = sizeof(struct CHAR);

 

In this example x is 4 and y is 1.  I would expect X to be 1.  Is this a bug?  Is there a work around?

 

0 Kudos
Message 1 of 3
(4,201 Views)

I suppose the reason is that you are mapping bits in an unsigned int: try using an unsigned char instead.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 3
(4,182 Views)

I hadn't thought of that.  The ISO standard says that bit fields have to be declared as "unisgned int", "signed int", or "int".  Nonetheless, that worked!  Thanks!

0 Kudos
Message 3 of 3
(4,180 Views)