Results 1 to 9 of 9

Thread: failing to achieve desired size of a BitField structure

  1. #1
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default failing to achieve desired size of a BitField structure

    hello everyone,
    i read somewhere that it is possible to define a BitFields structure
    so i set out to:

    Qt Code:
    1. struct IC_X
    2. {
    3. unsigned short int dump:2;
    4. unsigned short int ic1:10;
    5. unsigned short int ic2:10;
    6. unsigned short int ic3:10;
    7. };
    8. struct EVS
    9. {
    10. unsigned short int dump:2;
    11. unsigned short int v:10;
    12. unsigned short int p:10;
    13. unsigned short int q:10;
    14. };
    15. struct SE_P
    16. {
    17. unsigned short int dump:1;
    18. unsigned short int c1s:1;
    19. unsigned short int c2s:1;
    20. unsigned short int c3s:1;
    21. unsigned short int c1e:1;
    22. unsigned short int c2e:1;
    23. unsigned short int c3e:1;
    24. unsigned short int rC:1;
    25. };
    26. struct ALS
    27. {
    28. unsigned short int dump:5;
    29. unsigned short int al1:1;
    30. unsigned short int al3:1;
    31. unsigned short int al4:1;
    32. };
    To copy to clipboard, switch view to plain text mode 
    then
    Qt Code:
    1. struct LogEntry
    2. {
    3. IC_X icx;
    4. EVS evs;
    5. SE_P sep;
    6. ALS als;
    7. FileTime TimeStamp;
    8. };
    To copy to clipboard, switch view to plain text mode 

    FileTime is just a typedef for an unsigned long.
    dump fields are NOT used.

    so basically i wanted to pack 3, 10-bit numbers in IC_X (and leave the 2 MSB bits unused), then another 3 10-bit nums in EVS, then 7 bools in SE_P, and another 3 bools in ALS.
    finally pack them all together (along with an unsigned long timeStamp) to a 14byte long structure.
    with this structure though i get a sizeof(LogEntry)=20 in execution...

    what am i missing?
    thank you for your help
    nass

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: failing to achieve desired size of a BitField structure

    As far as I know, gcc will align structs and certain types on word boundaries. (For example to speed up access)

    To avoid that read about the Type Attribute "Pack"

    Hope it helps :-)

  3. #3
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Unhappy Re: failing to achieve desired size of a BitField structure

    hm perhpas i should mention
    that i us g++... not gcc... i hope that doesn't mees everything up... eh??
    please tell me it doesn't ...!

  4. #4
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: failing to achieve desired size of a BitField structure

    I personally think that it shouldn't matter, as gcc and g++ are very much related :-)

    If you go up one level in the documentation you will find the following nugget:
    These extensions are available in C and Objective-C. Most of them are also available in C++.
    I would suggest: just try it out ;-)
    Last edited by camel; 13th February 2007 at 11:11. Reason: updated contents

  5. #5
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: failing to achieve desired size of a BitField structure

    so i added in my structures:

    29: struct IC_X __attribute__ ((__packed__))
    30: {
    31: unsigned short int dump:2;
    32: unsigned short int ic1:10;
    33: unsigned short int ic2:10;
    34: unsigned short int ic3:10;
    35: };

    but it didnt compile. it would give me the errors:

    logger.h:30: error: syntax error before `{' token
    logger.h:32: error: syntax error before `:' token
    logger.h:33: error: syntax error before `:' token
    logger.h:34: error: syntax error before `:' token

    so i surfed around some more for bitfields and c++ and in
    http://msdn2.microsoft.com/en-us/library/ms858673.aspx

    i found that i shouldn't be declaring types.. now IC_X looks like:
    struct IC_X
    {
    unsigned dump:2;
    unsigned ic1:10;
    unsigned ic2:10;
    unsigned ic3:10;
    };

    and so do all the rest. it compiles and i get sizeof(IC_X)=4.. so OK.

    BUT

    the structures

    struct SE_P
    {
    unsigned dump:1;
    unsigned c1s:1;
    unsigned c2s:1;
    unsigned c3s:1;
    unsigned c1e:1;
    unsigned c2e:1;
    unsigned c3e:1;
    unsigned rC:1;
    };
    struct ALS
    {
    unsigned dump:5;
    unsigned al1:1;
    unsigned al3:1;
    unsigned al4:1;
    };

    which i would like them to be of size 1byte... are not, they also return sizeof(ALS)=4...

    so i'm still looking on how to fix these 2.
    smth like __attribute__ ((__packed__)) would work nicely here... but it seems g++ has a problem with it..
    any other pointers you might have are welcome
    nass

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: failing to achieve desired size of a BitField structure

    Try:
    Qt Code:
    1. struct IC_X
    2. {
    3. unsigned short int dump:2;
    4. unsigned short int ic1:10;
    5. unsigned short int ic2:10;
    6. unsigned short int ic3:10;
    7. } __attribute__ ((__packed__));
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: failing to achieve desired size of a BitField structure

    finally what did the trick to get sizeof(LogEntry)=14... :

    Qt Code:
    1. struct LogEntry
    2. {
    3. unsigned ic1:10;
    4. unsigned ic2:10;
    5. unsigned ic3:10;
    6. unsigned dm1:2;//no name is for padding. :0 is for the next field to start in a new storage unit
    7.  
    8. unsigned v:10;
    9. unsigned p:10;
    10. unsigned q:10;
    11. unsigned dm2:2;
    12.  
    13. unsigned c1s:1;
    14. unsigned c2s:1;
    15. unsigned c3s:1;
    16. unsigned c1e:1;
    17. unsigned c2e:1;
    18. unsigned c3e:1;
    19. unsigned rC:1;
    20. unsigned dm3:1;
    21.  
    22. unsigned al1:1;
    23. unsigned al3:1;
    24. unsigned al4:1;
    25. unsigned dm4:5;
    26.  
    27. unsigned long TimeStamp;
    28. } __attribute__ ((__packed__));
    To copy to clipboard, switch view to plain text mode 

    without the packed attribute i'd get a sizeof 16.
    'dm*' are the dummy bits.

    Thank you both for your help!
    nass

  8. #8
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: failing to achieve desired size of a BitField structure

    any idea if timestamp will be LSB or MSB in intel processor when i will compile this structure???

  9. #9
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: failing to achieve desired size of a BitField structure

    The most cross-platform thing to do would be to check
    Q_BYTE_ORDER being equal to Q_BIG_ENDIAN or Q_LITTLE_ENDIAN and do the appropriate thing. Such as:
    Qt Code:
    1. #if Q_BYTE_ORDER == Q_BIG_ENDIAN
    2. //Do something
    3. #else
    4. //Do something else
    5. #endif
    To copy to clipboard, switch view to plain text mode 

    But if you only need intel, you should be safe to only consider little-endian

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.